clearInterval()之后怎么再回复定时器呀?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <!-- TemplateBeginEditable name="doctitle" --> <title>计时器</title> </head> <body> <script type="text/javascript"> function clock() { var time = new Date(); document.getElementById("clock").value=time; } function start() { i = setInterval("clock()",1000); } var i = setInterval("clock()",1000); </script> <form> <input type="text" id="clock" size="50px" /> <input type="button" value="start" onclick="start()" /> <input type="button" value="stop" onclick="clearInterval(i)" /> </form> </body> </html>
如果是按楼上这段代码运行,打开网页后先按start键后再按stop键就无法停止,可以改成
function start(){
clearInterval(i);
i = setInterval(clock,1000);
}
每次调用start在执行setIntertval之前先clearInterval,否侧会出现clearInterval失效,可以查看一下setInterval返回值,每次都不一样
这是我的方法,你可以试一试
新建一个setInterval