第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在文本框中輸入值時(shí),在單擊按鈕時(shí)創(chuàng)建倒數(shù)計(jì)時(shí)器:Javascript

在文本框中輸入值時(shí),在單擊按鈕時(shí)創(chuàng)建倒數(shù)計(jì)時(shí)器:Javascript

元芳怎么了 2022-05-22 11:15:28
我遇到了一個(gè)問(wèn)題,比如當(dāng)我嘗試輸入一個(gè)像 30 這樣的數(shù)字,然后倒數(shù)到 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>我認(rèn)為問(wèn)題出在 if else 語(yǔ)句中,我不確定我是否讓用戶(hù)輸入正確。
查看完整描述

3 回答

?
Helenr

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超4個(gè)贊

只需在 startTimer() 開(kāi)始時(shí)將“秒”分配給 obj.display.value 的當(dāng)前值,并確保為秒輸入一個(gè)“數(shù)字”類(lèi)型和一個(gè)起始值。


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


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>


查看完整回答
反對(duì) 回復(fù) 2022-05-22
?
慕森卡

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超8個(gè)贊

你可以使用這樣的東西:


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


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>


查看完整回答
反對(duì) 回復(fù) 2022-05-22
?
拉丁的傳說(shuō)

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊

<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(個(gè)位數(shù)輸入),如果您想輸入兩位數(shù)或更大的數(shù)字,可以添加去抖動(dòng)


查看完整回答
反對(duì) 回復(fù) 2022-05-22
  • 3 回答
  • 0 關(guān)注
  • 102 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)