зеркало из https://github.com/mozilla/pluotsorbet.git
35 строки
1.0 KiB
JavaScript
35 строки
1.0 KiB
JavaScript
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
|
|
|
'use strict';
|
|
|
|
// Only add setZeroTimeout to the window object, and hide everything
|
|
// else in a closure.
|
|
(function() {
|
|
var timeouts = [];
|
|
var messageName = "zero-timeout-message";
|
|
|
|
// Like setTimeout, but only takes a function argument. There's
|
|
// no time argument (always zero) and no arguments (you have to
|
|
// use a closure).
|
|
function setZeroTimeout(fn) {
|
|
timeouts.push(fn);
|
|
window.postMessage(messageName, "*");
|
|
}
|
|
|
|
function handleMessage(event) {
|
|
if (event.source == window && event.data == messageName) {
|
|
event.stopPropagation();
|
|
if (timeouts.length > 0) {
|
|
var fn = timeouts.shift();
|
|
fn();
|
|
}
|
|
}
|
|
}
|
|
|
|
window.addEventListener("message", handleMessage, true);
|
|
|
|
// Add the one thing we want added to the window object.
|
|
window.setZeroTimeout = setZeroTimeout;
|
|
})();
|