最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is the jQuery trigger function guaranteed to be synchronous - Stack Overflow

programmeradmin3浏览0评论

The trigger function appears to be synchronous. That is, all bound functions appear to be executed in sequence, synchronously (the invoked function may do something asynchronously, but that's not the question).

Is this true for custom and non-custom(click, hover, etc) events? Are there any events where the single threaded guarantee of Javascript does not hold true?

If it is true, can the behavior be changed to execute them asynchronously without inserting timeouts into each bound function?

Here is a sample which demonstrates The question:

var ele = $('#blah');
// Example of the bound function doing something synchronously
ele.bind('customEvent.sync', function() {
    // Do something synchronously
    window.foo = 'foo';
});

// Example of the bound function doing something asynchronously
ele.bind('customEvent.async', function() {
    window.setTimeout(function() {
        // Do something asynchronously
        window.bar = 'bar';
    }, 0);
});

// Trigger both events
ele.trigger('customEvent');

// If trigger is guaranteed to be synchronous this should alert 'foo:undefined' or possibly 'foo:bar' depending on whether the asych function was called first
// If trigger is NOT synchronous 'undefined:undefined', 'foo:undefined', 'undefined:bar', or 'foo:bar' could be alerted
alert(window.foo + ':' + window.bar);

UPDATE See: Is JavaScript guaranteed to be single-threaded? Custom events are guaranteed to be synchronous because of the single threaded nature of Javascript. Some built in event types may not be synchronous due to browser inconsistencies.

The trigger function appears to be synchronous. That is, all bound functions appear to be executed in sequence, synchronously (the invoked function may do something asynchronously, but that's not the question).

Is this true for custom and non-custom(click, hover, etc) events? Are there any events where the single threaded guarantee of Javascript does not hold true?

If it is true, can the behavior be changed to execute them asynchronously without inserting timeouts into each bound function?

Here is a sample which demonstrates The question:

var ele = $('#blah');
// Example of the bound function doing something synchronously
ele.bind('customEvent.sync', function() {
    // Do something synchronously
    window.foo = 'foo';
});

// Example of the bound function doing something asynchronously
ele.bind('customEvent.async', function() {
    window.setTimeout(function() {
        // Do something asynchronously
        window.bar = 'bar';
    }, 0);
});

// Trigger both events
ele.trigger('customEvent');

// If trigger is guaranteed to be synchronous this should alert 'foo:undefined' or possibly 'foo:bar' depending on whether the asych function was called first
// If trigger is NOT synchronous 'undefined:undefined', 'foo:undefined', 'undefined:bar', or 'foo:bar' could be alerted
alert(window.foo + ':' + window.bar);

UPDATE See: Is JavaScript guaranteed to be single-threaded? Custom events are guaranteed to be synchronous because of the single threaded nature of Javascript. Some built in event types may not be synchronous due to browser inconsistencies.

Share Improve this question edited Jul 17, 2021 at 13:20 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Dec 24, 2012 at 4:17 KyleKyle 1,0191 gold badge10 silver badges19 bronze badges 18
  • be a lot easier to follow if you gave example of what your concerns are – charlietfl Commented Dec 24, 2012 at 4:22
  • My concern is whether I can rely on the bound functions called my trigger to be executed before the line following the call to trigger – Kyle Commented Dec 24, 2012 at 4:27
  • 2 Yes, the call is synchronous regardless of any asynchronous processes that might be started in the triggered event handler. When the triggered handler returns, execution reverts to the statement following .trigger(...). ie, .trigger() is a glorified function call. – Beetroot-Beetroot Commented Dec 24, 2012 at 4:50
  • 1 Current thread is absolutely guaranteed to run to completion before anything put in place with setTimeout(), even if the timeout duration is 0. Same with ajax - a thread that calls, say, $.ajax(...) is guaranteed to run to completion before any of the success/error/complete handlers commences, even of the server was to respond immediately. – Beetroot-Beetroot Commented Dec 24, 2012 at 5:03
  • 1 It's inherent to ECMA-262 (javascript), which is strictly single-threaded, ie. at any moment in time, a maximum of one thread can be running. – Beetroot-Beetroot Commented Dec 24, 2012 at 5:21
 |  Show 13 more comments

1 Answer 1

Reset to default 20

A lightly modified anthology of comments offered above by myself - Beetroot-Beetroot :

  • Events have no duration; providing a handler is attached, they cause a thread to start. Therefore an event, in itself, can be neither synchronous nor asynchronous. The thread that an event stimulates will always be synchronous but may kick off one or more asynchronous processes.

  • A call to .trigger() is always synchronous regardless of any asynchronous processes that might be started in the triggered event handler. When the triggered handler returns, execution reverts to the statement following .trigger(...). ie, .trigger() is a glorified function call.

  • In javascript, the current thread is absolutely guaranteed to run to completion before anything put in place with setTimeout() starts, even if the timeout duration is 0. Same with ajax - a thread that calls, say, $.ajax(...) is guaranteed to run to completion before any of the success/error/complete handlers commences, even if the server was to respond immediately.

  • Single-threadedness is inherent to ECMA-262 (javascript), ie. at any moment in time, a maximum of one thread can be running.

Additional point :

The acceped answer to Is javascript guaranteed to be single-threaded? is based on a false premise. No evidence is offered that javascript is not single-threaded. The comment by @KrisGiesing debunks the misunderstanding.

发布评论

评论列表(0)

  1. 暂无评论