为javascript循环执行实现暂停和恢复机制
Implementing a pause and resume mechanism for javascript loop execution
我正在尝试为递归调用的循环提供暂停和恢复功能。我正在使用"setTimeout"answers"clearTimeout"函数。。。
当用户单击"暂停"按钮时,计时器将设置为无限长时间(尽可能长)。当用户点击"恢复"按钮时,超时应该通过"clearTimeout"调用来清除。但超时永远不会被清除。我犯了什么错?
提前谢谢。。
JFIDDLE演示
HTML:
<button id="loop_controler">pause</button>
JS:
var array = [1,2,3,4,5,6,7];
var pause = false;
var timer;
function execute(array,i){
var pauseTime = 0;
if (pause){
pauseTime = 2147483647; //max value for timeout
}
timer = setTimeout(function(){ //set timer
setTimeout(function () {
alert(array[i]); //this timeout gives a constant delay
if (array.length > i) //(just to give enough time to users to click button)
execute(array,i+1);
}, 1000);
}, pauseTime);
console.log('set ' + timer);
}
$('document').ready(function(){
$('#loop_controler').click(function(){
if ($(this).text() == 'pause'){
pause = true;
$(this).text('resume');
} else {
clearTimeout(timer); //clear timer
console.log('clear ' + timer);
pause = false;
$(this).text('pause');
}
});
execute(array,0);
});
早在2012年,我就用JavaScript编写了一段用于增量计时的代码片段,该代码使用setTimeout
,并具有start
和stop
函数:https://gist.github.com/aaditmshah/2056987
查看演示:
var button = document.querySelector("button");
var div = document.querySelector("div");
var running = false;
var number = 1;
var timer = new DeltaTimer(function () {
div.innerHTML = number++;
}, 1000);
button.addEventListener("click", function () {
if (running) {
timer.stop();
button.innerHTML = "Start";
running = false;
} else {
timer.start();
button.innerHTML = "Stop";
running = true;
}
});
function DeltaTimer(render, interval) {
var timeout;
var lastTime;
this.start = start;
this.stop = stop;
function start() {
timeout = setTimeout(loop, 0);
lastTime = Date.now();
return lastTime;
}
function stop() {
clearTimeout(timeout);
return lastTime;
}
function loop() {
var thisTime = Date.now();
var deltaTime = thisTime - lastTime;
var delay = Math.max(interval - deltaTime, 0);
timeout = setTimeout(loop, delay);
lastTime = thisTime + delay;
render(thisTime);
}
}
<button>Start</button>
<div></div>
希望能有所帮助。
我误解了clearTimeout函数。我认为回调(setTimeout(…)中的calback)会在调用clearTimeout之后立即执行。但在调用clearTimeout后,回调不会执行。。。
工作JS代码是,
var array = [1,2,3,4,5,6,7];
var timer;
var i=0;
function execute(){
timer = setTimeout(function () {
alert(array[i]);
i++;
if (array.length > i){
execute();
}
}, 1000);
}
$('document').ready(function(){
$('#loop_controler').click(function(){
if ($(this).text() == 'pause'){
clearTimeout(timer);
$(this).text('resume');
} else {
execute();
$(this).text('pause');
}
});
execute(array,0);
});
相关文章:
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- 如何临时暂停浏览器渲染,然后恢复整个页面
- NodeJS-readline暂停和恢复事件发射器(逐行读取)
- 谷歌地图劫持了iphone's滚动(触摸事件)-如何恢复
- 我希望在不替换现有变量的情况下恢复localStorage中的变量
- 克隆和恢复”;工具化的“;元素
- REST的面向公众的身份验证机制
- ES6是否引入了一种机制来生成块范围的函数语句(而不是表达式)
- 如何暂停和恢复jquery间隔
- 在AngularJs中添加轮询机制
- 在python中,我如何才能恢复被隐藏的元素的内容
- javascript恢复样式
- 在意外断电时恢复JS计时器
- 将JSON存储和恢复到此Ionic应用程序的最有效方法
- 在文本区域禁用javascript定义的keydown事件并恢复默认行为
- 将嵌套的主干模型存储并恢复到本地存储中
- 当客户端在JW Player中恢复丢失的连接时,会自动重新连接
- 从jquery对话框恢复原始数据,脚本不起作用
- 如何恢复Kinetic.js鼠标事件
- 为javascript循环执行实现暂停和恢复机制