Javascript睡眠/延迟/等待功能 [英] Javascript sleep/delay/wait function
问题描述
对不起,如果这个问题已经在这里问过,我找不到合适的答案。
我想要创建一个JavaScript睡眠/延迟/等待函数我可以调用脚本中的任何地方,比如jQuery的 .delay()
我无法使用setTimeout ,因为我有一个由php生成的脚本,所以我不能将它放到两个不同的函数中,在中间超时。我需要创建一个允许我做的函数
alert(time started);
sleep(4000);
alert(time up);
I 真的不想使用jQuery。
不幸的是,你不能只是放入一个函数来使Javascript停顿。
例如:
函数startTimer(){
timer.start();
setTimeout(stopTimer,5000);
}
函数stopTimer(){
timer.stop();
编辑:
为了您的用户生成倒计时,它就是一样简单。
HTML:
< input type =numberid =延迟min =1max =5>
JS:
var delayInSeconds = parseInt(delay.value);
var delayInMilliseconds = delayInSeconds * 1000;
函数startTimer(){
timer.start();
setTimeout(stopTimer,delayInMilliseconds);
}
function stopTimer(){
timer.stop;
$ / code>
现在您只需添加的触发器startTimer ()
,比如 onchange
。
Sorry if this question has already been asked here before, I could not find a suitable answer.
I am wanting to create a JavaScript sleep/delay/wait function that I can call anywhere in the script, like jQuery's .delay()
I am not able to use setTimeout, as I have a script that is generated by php, and so am not able to put it into two different functions, with the timeout in the middle. I need to create a function that allows me to do
alert("time started");
sleep(4000);
alert("time up");
I really do not want to use jQuery.
You cannot just put in a function to pause Javascript unfortunately.
You have to use setTimeout()
Example:
function startTimer () {
timer.start();
setTimeout(stopTimer,5000);
}
function stopTimer () {
timer.stop();
}
EDIT:
For your user generated countdown, it is just as simple.
HTML:
<input type="number" id="delay" min="1" max="5">
JS:
var delayInSeconds = parseInt(delay.value);
var delayInMilliseconds = delayInSeconds*1000;
function startTimer () {
timer.start();
setTimeout(stopTimer,delayInMilliseconds);
}
function stopTimer () {
timer.stop;
}
Now you simply need to add a trigger for startTimer()
, such as onchange
.
这篇关于Javascript睡眠/延迟/等待功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!