JavaScript中clearTimeout()函数的作用是什么?
如果使用setTimeout() 函数设置了时间,则可以使用JavaScriptclearTimeout()函数将其清除。
示例
您可以尝试运行以下代码,了解如何clearTimeout()在JavaScript中实现功能-
<html>
<head>
<title>JavaScript Animation</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>热门推荐
7 合字的祝福语简短
10 简短大气的狗年祝福语
11 儿女的生日祝福语简短
12 新婚贺词简短的祝福语
13 学生开学季祝福语简短
14 姐妹结婚快乐祝福语简短
15 过节文案成语祝福语简短
16 圣诞祝福语简短小学
17 青春告白祖国简短祝福语
18 六一互换礼物祝福语简短