Editing goog.timer.callImmediate This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) var goog = { Timer: { defaultTimerObject: window }, bind: function(fn, context) { return fn.bind(context); } }; goog.Timer.getSetImmediate_ = function() { // Introduced and currently only supported by IE10. if (typeof goog.Timer.defaultTimerObject['setImmediate'] === 'function') { return goog.bind(goog.Timer.defaultTimerObject['setImmediate'], goog.Timer.defaultTimerObject); } // Create a private message channel and use it to postMessage empty messages // to ourselves. var Channel = goog.Timer.defaultTimerObject['MessageChannel']; // If MessageChannel is not available, implement an iframe based polyfill // in browsers that have postMessage and document.createEvent. The latter // excludes IE8 because it has a synchronous postMessage implementation. if (typeof Channel === 'undefined' && window.postMessage && document.createEvent) { Channel = function() { var iframe = document.createElement('iframe'); iframe.style.display = 'none'; iframe.src = ''; document.body.appendChild(iframe); var win = iframe.contentWindow var doc = win.document; doc.open(); doc.write(''); doc.close(); var message = 'callImmediate' + Math.random(); var origin = win.location.protocol + '//' + win.location.host; iframe.contentWindow.onmessage = goog.bind(function(e) { if (e.origin != origin && e.data != message) { return; } this['port1'].onmessage() }, this); this['port1'] = {}; this['port2'] = { postMessage: function() { win.postMessage(message, origin); } }; }; } if (typeof Channel !== 'undefined') { var channel = new Channel(); // Use a fifo linked list to call callbacks in the right order. var head = {}; var tail = head; channel['port1'].onmessage = function() { head = head.next; var cb = head.cb; head.cb = null; cb(); }; return function(cb) { tail.next = { cb: cb }; tail = tail.next; channel['port2'].postMessage(0); }; } // Fall back to setTimeout. return function(cb) { goog.Timer.defaultTimerObject.setTimeout(cb, 0); } }; goog.Timer.callImmediate = function(listener, opt_handler) { var callback = listener; if (opt_handler) { callback = goog.bind(listener, opt_handler); } if (!goog.Timer.setImmediate_) { goog.Timer.setImmediate_ = goog.Timer.getSetImmediate_(); } goog.Timer.setImmediate_(callback); }; Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code goog.Timer.callImmediate(function() { deferred.resolve(); }); Test 2 Title Async (check if this is an asynchronous test) Code setTimeout(function() { deferred.resolve(); }, 0)