
要在JavaScript中停止函数的执行,请使用clearTimeout()方法。此函数调用会清除由setTimeout()函数设置的任何定时器。
示例
您可以尝试运行以下代码,了解如何在JavaScript中使用clearTimeout()方法。
<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>
Java免费学习笔记:立即使用
解锁 Java 大师之旅:从入门到精通的终极指南











