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 }, global: window, bind: function(fn, context) { return fn.bind(context); } }; goog.Timer.getSetImmediate_ = function() { // Introduced and currently only supported by IE10. if (typeof goog.global['setImmediate'] === 'function') { return goog.bind(goog.global['setImmediate'], goog.global); } // Create a private message channel and use it to postMessage empty messages // to ourselves. var Channel = goog.global['MessageChannel']; // If MessageChannel is not available and we are in a browser, 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' && typeof window !== 'undefined' && window.postMessage && document.createEvent) { /** @constructor */ Channel = function() { // Make an empty, invisible iframe. 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) { // Validate origin and message to make sure that this message was // intended for us. 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); }; } // Implementation for IE6-8. Script elements fires an asynchronous // onreadystatechange event when inserted into the DOM. if (typeof document !== 'undefined' && 'onreadystatechange' in document.createElement('script')) { return function(cb) { var script = document.createElement('script'); script.onreadystatechange = function() { // Clean up and run the callback. script.onreadystatechange = null; script.parentNode.removeChild(script); script = null; cb(); cb = null; }; document.documentElement.appendChild(script); }; } // Fall back to setTimeout with 0. In browsers this creates a delay of 5ms // or more. return function(cb) { goog.global.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)