Skip to content

Instantly share code, notes, and snippets.

@jussi-kalliokoski
Created January 22, 2012 14:48
Show Gist options
  • Save jussi-kalliokoski/1657272 to your computer and use it in GitHub Desktop.
Save jussi-kalliokoski/1657272 to your computer and use it in GitHub Desktop.
A small shim for the setImmediate() that creates a callback similar to process.nextTick() on node.js
(function (global, prefixes, timeouts, id, i) {
if (typeof process !== 'undefined' && process && process.nextTick) {
global.setImmediate = function (callback, args) {
var i = id;
timeouts[id] = {
c: callback,
a: args || [],
};
process.nextTick(function () {
var t = timeouts[i];
if (t) {
t.c.apply(global, t.a);
delete timeouts[i];
}
});
return id++;
};
global.clearImmediate = function (id) {
timeouts[id] && delete timeouts[id];
};
return;
}
for (i=0; !global.setImmediate && i<prefixes.length; i++) {
global.setImmediate = global[prefixes[i] + 'SetImmediate'];
global.clearImmediate = global[prefixes[i] + 'ClearImmediate'];
}
if (!global.setImmediate && global.postMessage && global.addEventListener) {
global.addEventListener('message', function (e) {
if (!e.data || e.source !== global || !e.data.setImmediate) return;
e.preventDefault && e.preventDefault();
e.stopPropagation && e.stopPropagation();
var t = timeouts[e.data.setImmediate];
if (t) {
t.c.apply(global, t.a);
delete timeouts[e.data.setImmediate];
}
}, true);
global.setImmediate = function (callback, args) {
timeouts[id] = {
c: callback,
a: args || [],
};
global.postMessage({
setImmediate: id,
}, '*');
return id++;
};
global.clearImmediate = function (id) {
timeouts[id] && delete timeouts[id];
};
return;
}
if (!global.setImmediate && global.setInterval) {
global.setImmediate = function (callback, args) {
return global.setTimeout(callback, 0, args);
};
global.clearImmediate = global.clearTimeout;
return;
}
}(typeof global !== 'undefined' && global ? global : this, ['webkit', 'moz', 'ms', 'o'], {}, +new Date));
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="r"></div>
<script src="setImmediate.js"></script>
<script>
function printResult () {
var e = document.createElement('div');
e.appendChild(document.createTextNode([].slice.call(arguments).join(' ')));
document.getElementById('r').appendChild(e);
}
var start = +new Date;
setImmediate(function (s) {
printResult('It worked and took', +new Date - s, 'milliseconds.');
}, [start]);
var canceling = setImmediate(function () {
printResult('ERROR: this should not have appeared.');
});
printResult('Attempting to cancel immediate with id:', canceling);
clearImmediate(canceling);
</script>
</body>
</html>
// Test for node.js and jsshell (unsupported because it doesn't even have setTimeout)
typeof require === 'undefined' ? load('setImmediate.js') : require('./setImmediate.js');
function printResult () {
var msg = [].slice.call(arguments).join(' ');
typeof print === 'undefined' ? console.log(msg) : print(msg);
}
var start = +new Date;
setImmediate(function (s) {
printResult('It worked and took', +new Date - s, 'milliseconds.');
}, [start]);
var canceling = setImmediate(function () {
printResult('ERROR: this should not have appeared.');
});
printResult('Attempting to cancel immediate with id:', canceling);
clearImmediate(canceling);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment