Javascript setTimeout js函数只工作一次
Javascript setTimeout js函数只工作一次,javascript,Javascript,setTimeout()函数只运行一次。它应该每100毫秒刷新一次。但是youtube教程使用setTimeout。为什么它不在我的工作?无法为此应用程序使用setTimeout吗 <html> <head> <title>Stop Watch</title> <script type="text/javascript"> var time=0; var running=1; function run(){ if(
setTimeout()
函数只运行一次。它应该每100毫秒刷新一次。但是youtube教程使用setTimeout。为什么它不在我的工作?无法为此应用程序使用setTimeout吗
<html>
<head>
<title>Stop Watch</title>
<script type="text/javascript">
var time=0;
var running=1;
function run(){
if(running==1){
setTimeout(function(){
time++;
document.getElementById("lead").innerHTML="Time: "+time;
},100);
}
}
</script>
</head>
<body>
<p id="lead">Click to start Timer</p>
<button id="startPause" onclick="run()">Start</button>
<button id="reset" onclick="">Reset</button>
</body>
</html>
秒表
var时间=0;
var运行=1;
函数运行(){
如果(运行==1){
setTimeout(函数(){
时间++;
document.getElementById(“lead”).innerHTML=“时间:”+时间;
},100);
}
}
单击启动计时器
开始
重置
将重复任务的设置超时
替换为设置间隔
function run(){
if(running==1){
window.setInterval(function(){
time++;
document.getElementById("lead").innerHTML="Time: "+time;
//run();
},1000);
}
else {
time=0;
window.clearInterval(interval);
}
}
您可能正在寻找
setInterval
setInterval()是一种方法,正如@elclanrs.yeahthanx@elclanrs所提到的,setInterval是有效的。但是youtube教程使用setTimeout。为什么它不在我的工作…是的,它工作:)Thanx这将创建一个间隔每100秒。。祝你好运,破坏用户的IE。这只是示例代码。可根据需要进行更改。。。建设性的意见是删除结尾附近的递归run()
,我没有做任何事情@Vivek。。。