要在javascript中停止函数的执行,请使用cleartimeout()方法。此函数调用会清除由settimeout()函数设置的任何定时器。
示例您可以尝试运行以下代码,了解如何在javascript中使用cleartimeout()方法。
<html> <head> <title>javascript cleartimeout() method</title> <script> <!-- var imgobj = null; var animate ; function init(){ imgobj = document.getelementbyid('myimage'); imgobj.style.position= 'relative'; imgobj.style.left = '0px'; } function moveright(){ imgobj.style.left = parseint(imgobj.style.left) + 10 + 'px'; animate = settimeout(moveright,20); // call moveright in 20msec } function stop(){ cleartimeout(animate); imgobj.style.left = '0px'; } window.onload = init; //--> </script> </head> <body> <form> <img id = "myimage" src = "/images/html.gif" /> <p>click the buttons below to handle animation</p> <input type = "button" value = "start" onclick = "moveright();" /> <input type = "button" value = "stop" onclick = "stop();" /> </form> </body></html>
以上就是如何使用javascript停止函数的执行?的详细内容。
