在文本框中输入值时,在单击按钮时创建倒数计时器:Javascript

我遇到了一个问题,比如当我尝试输入一个像 30 这样的数字,然后倒数到 0,但它不起作用。


<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>123</title>

<script type="text/javascript">




function startTimer()

{


   seconds = seconds - 1;

   if (seconds <= 0)    

   {

      seconds = 0;

   }

    else

        {

            seconds--;

        }


   var obj = document.getElementById("timer");

   obj.display.value= seconds;

}


</script>

</head>

<body>

    <form id="timer" action="#">

<p><input type="text" name="display" size="

    20" /></p>

<p><input type="button" value="Start"

    onclick="Id=setInterval('startTimer()', 100)" />

</form>

</script>

</body>

</html>

我认为问题出在 if else 语句中,我不确定我是否让用户输入正确。


元芳怎么了
浏览 127回答 3
3回答

Helenr

只需在 startTimer() 开始时将“秒”分配给 obj.display.value 的当前值,并确保为秒输入一个“数字”类型和一个起始值。完成后也使用 clearInterval(Id) 停止计时器。function startTimer(){&nbsp; &nbsp;var obj = document.getElementById("timer");&nbsp; &nbsp; /* make sure to tell javascript that 'seconds' is&nbsp; Number that&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; comes from the input box */&nbsp; &nbsp;var seconds;&nbsp; &nbsp;seconds = Number(obj.display.value);/*&nbsp; Don't need this *AND* seconds-- *///&nbsp; &nbsp; &nbsp; &nbsp;seconds = seconds - 1;&nbsp; &nbsp;if (seconds <= 0)&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{&nbsp; &nbsp;clearInterval(Id);&nbsp; &nbsp; &nbsp; seconds = 0;&nbsp; &nbsp;}&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; seconds--;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; obj.display.value = seconds;}<!doctype html><html><head><meta charset="utf-8"><title>123</title><script type="text/javascript"></script></head><body>&nbsp; &nbsp; <form id="timer" action="#"><p><input type="number" name="display"&nbsp; size="&nbsp; &nbsp; 20" value="30" /></p><!-- changed the interval from 100ms to 1000ms --><p><input type="button" value="Start"&nbsp; &nbsp; onclick="Id=setInterval('startTimer()', 1000)" /></form></script></body></html>

慕森卡

你可以使用这样的东西:将数字修改为您想要的任何数字,如果您想要一个input控件,那么我假设您知道该怎么做,如果不让我知道。function myFunction() {&nbsp;var inputVal =&nbsp; document.getElementById('myInput').value;&nbsp;var seconds = inputVal, $seconds = document.querySelector('#countdown');&nbsp;(function countdown() {&nbsp; &nbsp; &nbsp; &nbsp;$seconds.textContent = seconds + ' second' + (seconds == 1 ?&nbsp; '' :&nbsp; 's')&nbsp; &nbsp; &nbsp; &nbsp;if(seconds --> 0) setTimeout(countdown, 1000)&nbsp; &nbsp;})();}<input type="text" id="myInput" placeholder="Enter number..." ><button onclick="myFunction()">Start Counter</button>&nbsp;&nbsp;<span id="countdown"></span>

拉丁的传说

<input type="number" id="inp"><div id="counter"></div><script>let input = document.getElementById('inp')let counter = document.getElementById('counter')let handleInput = e => {&nbsp; let num = Number(e.target.value)&nbsp; let _counter = num - 1&nbsp; let timer = setInterval(_ => {&nbsp; if(!_counter)&nbsp; &nbsp; &nbsp;clearInterval(timer)&nbsp; &nbsp; counter.innerText =&nbsp; _counter&nbsp; &nbsp; _counter--&nbsp; }, 1000)}input.addEventListener('input', handleInput)</script>上述逻辑适用于 1 - 9(个位数输入),如果您想输入两位数或更大的数字,可以添加去抖动
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript