簡體   English   中英

如何在 javascript 中設置按鈕點擊計時器?

[英]How to Set a timer on button click in javascript?

我在點擊按鈕時設置了一個計時器

$(document).on("click", "[button]", function() {
        var button = document.getElementById('my_btn');
            var time = 5;
            var timer = setInterval(function() {
              if (time > 0) {
                time--;
                button.disabled = true;
                button.innerHTML = 'Please wait for '+time+ ' seconds' ;
                console.log(time);
              }
              if (time === 0) {
                button.disabled = false;
                button.innerHTML = 'Press Me' ;
              }
            }, 1000);
        });

這只運行一次。 當計時器結束時。 然后,當我再次點擊按鈕時。 它沒有顯示任何內容,也沒有任何反應。

在 if (time === 0) { 中清除你的間隔

if (time === 0) {
  button.disabled = false;
  button.innerHTML = 'Press Me' ;
  clearInterval(timer); // here
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM