暂停Javascript执行,直到按下按钮 [英] Pausing Javascript execution until button press

查看:166
本文介绍了暂停Javascript执行,直到按下按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的Algorithms类(使用Javascript)创建Sudoku创建器的可视化。该算法效果很好,但我无法找到暂停执行的方法。

I'm creating a visualization of a Sudoku creator for my Algorithms class (in Javascript). The algorithm works great, but I'm having trouble finding a way to pause execution.

目前,我使用 prompt() code>暂停,但这是庞大而烦人的。除了连续循环之外,是否有任何方法可以暂停直到另一个函数运行(通过HTML按钮)?

Currently, I'm using prompt() to pause, but that's bulky and annoying. Is there any way to pause until another function is run (via HTML button) other than a continuous while loop?

我可以发布代码,但我不认为这是必要的。

I can post code, but I don't think it's needed. I'm not currently using jQuery, but I can if needed.

推荐答案

var flag = true;
function foo(){
    if (flag){
        // Do your magic here
        ...
        ...
        setTimeout(foo, 100);
    }
}

function stop(){
    flag = false;
}
<input type="button" onclick="stop();" value="stop it!!!" />

直播DEMO

这篇关于暂停Javascript执行,直到按下按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆