亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在文本框中輸入值時,在單擊按鈕時創建倒數計時器:Javascript

在文本框中輸入值時,在單擊按鈕時創建倒數計時器:Javascript

元芳怎么了 2022-05-22 11:15:28
我遇到了一個問題,比如當我嘗試輸入一個像 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 語句中,我不確定我是否讓用戶輸入正確。
查看完整描述

3 回答

?
Helenr

TA貢獻1780條經驗 獲得超4個贊

只需在 startTimer() 開始時將“秒”分配給 obj.display.value 的當前值,并確保為秒輸入一個“數字”類型和一個起始值。


完成后也使用 clearInterval(Id) 停止計時器。


function startTimer()

{

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


    /* make sure to tell javascript that 'seconds' is  Number that 

        comes from the input box */

   var seconds;



   seconds = Number(obj.display.value);


/*  Don't need this *AND* seconds-- */

//       seconds = seconds - 1;


   if (seconds <= 0)    

   {

   clearInterval(Id);

      seconds = 0;

   }

    else

        {

            seconds--;

        }


  obj.display.value = seconds;

}

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>123</title>

<script type="text/javascript">





</script>

</head>

<body>

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

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

    20" value="30" /></p>


<!-- changed the interval from 100ms to 1000ms -->

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

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

</form>

</script>

</body>

</html>


查看完整回答
反對 回復 2022-05-22
?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

你可以使用這樣的東西:


將數字修改為您想要的任何數字,如果您想要一個input控件,那么我假設您知道該怎么做,如果不讓我知道。


function myFunction() {

 var inputVal =  document.getElementById('myInput').value;

 var seconds = inputVal, $seconds = document.querySelector('#countdown');

 (function countdown() {

       $seconds.textContent = seconds + ' second' + (seconds == 1 ?  '' :  's')

       if(seconds --> 0) setTimeout(countdown, 1000)

   })();

}

<input type="text" id="myInput" placeholder="Enter number..." >

<button onclick="myFunction()">Start Counter</button>  

<span id="countdown"></span>


查看完整回答
反對 回復 2022-05-22
?
拉丁的傳說

TA貢獻1789條經驗 獲得超8個贊

<input type="number" id="inp">

<div id="counter"></div>

<script>

let input = document.getElementById('inp')

let counter = document.getElementById('counter')


let handleInput = e => {

  let num = Number(e.target.value)

  let _counter = num - 1

  let timer = setInterval(_ => {

  if(!_counter)

     clearInterval(timer)

    counter.innerText =  _counter

    _counter--

  }, 1000)

}

input.addEventListener('input', handleInput)

</script>


上述邏輯適用于 1 - 9(個位數輸入),如果您想輸入兩位數或更大的數字,可以添加去抖動


查看完整回答
反對 回復 2022-05-22
  • 3 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號