这似乎有效:
(function () {
var timers = {};
var id = 0;
var maxT = 1000; // can go up to (1 << 30)
window.setLongTimeout = function (f, t) {
var args = [].slice.call(arguments, 2);
var now = (Date.now && Date.now()) || +(new Date);
var end = now + t;
var myid = ++id;
(function loop() {
var now = (Date.now && Date.now()) || +(new Date);
var delta = end - now;
if (delta <= 0) {
delete timers[myid];
f.apply(null, args);
} else {
timers[myid] = window.setTimeout(loop, Math.min(maxT, delta));
}
})();
return id;
};
window.clearLongTimeout = function (myid) {
window.clearTimeout(timers[myid]);
delete timers[myid];
};
})();
它会定期触发超时,直到最初指定的时间(不限于 32 位)已经过去。
如果您尝试使用它超过2 ^ 52
毫秒,预计会出现精度错误;-)
见http://jsfiddle.net/alnitak/zGdn5/