编辑:另一个答案是使用WebWorker
s 每个https://stackoverflow.com/a/12522580/1481489 - 这个答案有点具体,所以这里有一些更通用的东西:
间隔.js
var intervalId = null;
onmessage = function(event) {
if ( event.data.start ) {
intervalId = setInterval(function(){
postMessage('interval.start');
},event.data.ms||0);
}
if ( event.data.stop && intervalId !== null ) {
clearInterval(intervalId);
}
};
和你的主程序:
var stuff = { // your custom class or object or whatever...
first: Date.now(),
last: Date.now(),
callback: function callback() {
var cur = Date.now();
document.title = ((cur-this.last)/1000).toString()+' | '+((cur-this.first)/1000).toString();
this.last = cur;
}
};
var doWork = new Worker('interval.js');
doWork.onmessage = function(event) {
if ( event.data === 'interval.start' ) {
stuff.callback(); // queue your custom methods in here or whatever
}
};
doWork.postMessage({start:true,ms:250}); // tell the worker to start up with 250ms intervals
// doWork.postMessage({stop:true}); // or tell it just to stop.
完全丑陋,但你可以打开一个子弹出窗口。然而,这一切只是将一些警告转移到子窗口,即如果子窗口最小化,则会出现 1000 毫秒问题,但如果它只是失焦,则没有问题。再说一次,如果它是关闭的,那么它就会停止,但用户所要做的就是再次单击开始按钮。
所以,我想这并不能真正解决你的问题......但这是一个粗略的草案:
var mainIntervalMs = 250;
var stuff = { // your custom class or object or whatever...
first: Date.now(),
last: Date.now(),
callback: function callback(){
var cur = Date.now();
document.title = ((cur-this.last)/1000).toString()+' | '+((cur-this.first)/1000).toString();
this.last = cur;
}
};
function openerCallbackHandler() {
stuff.callback(); // queue your custom methods in here or whatever
}
function openerTick(childIntervalMs) { // this isn't actually used in this window, but makes it easier to embed the code in the child window
setInterval(function() {
window.opener.openerCallbackHandler();
},childIntervalMs);
}
// build the popup that will handle the interval
function buildIntervalWindow() {
var controlWindow = window.open('about:blank','controlWindow','width=10,height=10');
var script = controlWindow.document.createElement('script');
script.type = 'text/javascript';
script.textContent = '('+openerTick+')('+mainIntervalMs+');';
controlWindow.document.body.appendChild(script);
}
// write the start button to circumvent popup blockers
document.write('<input type="button" onclick="buildIntervalWindow();return false;" value="Start" />');
我建议制定一种更好的组织、写作等方法,但至少它应该为你指明正确的方向。它也应该在很多差异浏览器中工作(理论上,只在 chrome 中测试过)。剩下的就交给你了。
哦,别忘了在父窗口掉线时自动关闭子窗口。