如何使用JavaScript停止函数的执行?
要在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停止函数的执行?的详细内容,更多请关注双恒网络其它相关文章!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。



