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 | Show 13 more comments1 Answer
Reset to default 20A 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.
.trigger(...)
. ie,.trigger()
is a glorified function call. – Beetroot-Beetroot Commented Dec 24, 2012 at 4:50setTimeout()
, 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