暂停js脚本直到事件发生 [英] Suspending a js script until an event occurs

查看:58
本文介绍了暂停js脚本直到事件发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在诸如鼠标单击按钮之类的事件发生之前,是否有任何方法可以挂起js脚本?我的意思是脚本应暂停执行并在事件发生时再次恢复.我对Javascript还是陌生的,即使在网上进行了彻底的搜索也找不到解决方案!

Is there any way to suspend a js script until some event like a mouse click on a button occurs?? I mean the script should pause its execution and resume again when the event occurs. I am kind of new to Javascript and even after a thorough search on the net couldn't find a solution!!

推荐答案

JS是单线程的,多线程只能由Web Workers完成.据我所知.

JS is single threaded and multithreading can be done us Web Workers only. As far as I know.

但是,如果只有一个要挂起的小脚本,则可以有一个全局变量/标志,并且只需在脚本中(我不知何故认为它是循环或事件驱动函数)就可以检查该标志.在这种情况下:

However if you have only a little script which you want to suspend, you can have a global variable/flag and simply in your script(I somehow believe it's a loop or event driven function) have a check for that flag. In this case:

var flag = false;

...
if (flag) {
   do your code
} else {
   do nothing or return if in function
}

一旦要继续,只需设置 flag = true;

Once you want to continue, just set flag = true;

这篇关于暂停js脚本直到事件发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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