猿问

为啥越点击数字跳得越快?

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var num=0;
function numCount(){
 document.getElementById('txt').value=num;
 num=num+1;
 setTimeout("numCount()",5000);
 }
</script>
</head>
<body>
<form>
<input type="text" id="txt" />
<input type="button" value="Start" onClick="numCount()" />
</form>
</body>
</html>


像个好人
浏览 1516回答 3
3回答

Jerps

定时器一开启,需要关闭才会消失,按了多少次就相当于开了多少个定时器,自然会不断触发num++了。解决办法:在定时器函数外先清除上一个定时器,这样再触发也只有一个定时器了!

慕的地8582982

SUCIJQZHKFRXJXNZRKEUNKEBBUBKUOBDEKFJTAGWNXKIGXMMTCFPSTALOIPLIIBZFJQNPPPFMGMSTEBDNIRREZNTRLFGJQQDMDQFAYQQPLQYFRGBPYUJINEZMDRYVTRLHEFTGGWQAYBVYIYWFILZAGLOLSUQQTMHRUJAISEHAADKWTZKCFIBLEUHGUCLIBNBROEXCIGWRDNRPSVIHTPPJILOBMWDXJZZAAOXXOOZJZUQKNMJTTTQWAFIPPIZMNZPJXJTDOKUWVBZTWEWYFSZBEBLSYXDOYTFVQCMWMIFLAQOXSLRERXNTSFHOOSVELUIOLOYKHLKRUNDGKUNHJVSSGMUZCDDDGNDRMSZMMVFKQKLRXATGEFYYKQVPIIYQDKQHNJFPSSMCDWALHXNGAGTZQXEECJXVCUBBSRMMTAYGMWDBBBHIQIAKAYJUURLRFQYBZBZISWTKIICXKHBIDTUFCMNQORVGXBURLZUVFJKRBLYOWALEJRYSTMFTDPNYOCEMQHOGJVRDZOYOKVZFOXTWVOAOFIVPTPFLKGARVMJYVRREECVGDALVBPTKHLRLNOMNIZQNSJJBBSTTNRRZJUYSGNRQQHEVADYSIMZQZHVKPQUUBSPFDHYHCCUUJZZOGRBNEUIOMFWTFKUPPFTAZUDYYYVSZAGQGXUOYMCJJOHYFMWPDCCMJTQONSANKFVGGASMHKYVPMWDKLGQRFVHEPGTNADGLNSYZZQHHACQGIVOYQEKUNLBVCJBPUPZTNBGJBKYLSBLMZXUKBGHYSVAKEMAGOFHLMGYAOLDQOMNXYSNUFFJYLGTNYSFJZOCRTNOPSWWDBOLOZPTGXZZGESWPXCNRVBZUALYANYLIIIWROYDXRZJJXU

cnyballk

那是你点击的快吧,可以在前面写个清除定时,这样可以5秒后只跳一次
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答