react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / timers-browserify / main.js
80713 viewsvar nextTick = require('process/browser.js').nextTick;1var apply = Function.prototype.apply;2var slice = Array.prototype.slice;3var immediateIds = {};4var nextImmediateId = 0;56// DOM APIs, for completeness78exports.setTimeout = function() {9return new Timeout(apply.call(setTimeout, window, arguments), clearTimeout);10};11exports.setInterval = function() {12return new Timeout(apply.call(setInterval, window, arguments), clearInterval);13};14exports.clearTimeout =15exports.clearInterval = function(timeout) { timeout.close(); };1617function Timeout(id, clearFn) {18this._id = id;19this._clearFn = clearFn;20}21Timeout.prototype.unref = Timeout.prototype.ref = function() {};22Timeout.prototype.close = function() {23this._clearFn.call(window, this._id);24};2526// Does not start the time, just sets up the members needed.27exports.enroll = function(item, msecs) {28clearTimeout(item._idleTimeoutId);29item._idleTimeout = msecs;30};3132exports.unenroll = function(item) {33clearTimeout(item._idleTimeoutId);34item._idleTimeout = -1;35};3637exports._unrefActive = exports.active = function(item) {38clearTimeout(item._idleTimeoutId);3940var msecs = item._idleTimeout;41if (msecs >= 0) {42item._idleTimeoutId = setTimeout(function onTimeout() {43if (item._onTimeout)44item._onTimeout();45}, msecs);46}47};4849// That's not how node.js implements it but the exposed api is the same.50exports.setImmediate = typeof setImmediate === "function" ? setImmediate : function(fn) {51var id = nextImmediateId++;52var args = arguments.length < 2 ? false : slice.call(arguments, 1);5354immediateIds[id] = true;5556nextTick(function onNextTick() {57if (immediateIds[id]) {58// fn.call() is faster so we optimize for the common use-case59// @see http://jsperf.com/call-apply-segu60if (args) {61fn.apply(null, args);62} else {63fn.call(null);64}65// Prevent ids from leaking66exports.clearImmediate(id);67}68});6970return id;71};7273exports.clearImmediate = typeof clearImmediate === "function" ? clearImmediate : function(id) {74delete immediateIds[id];75};7677