簡體   English   中英

倒數計時腳本,無法正確顯示或倒數

[英]CountDown Script, Doesn't display or countdown correctly

所以我一直想調試我的番茄時鍾腳本。 我希望此腳本執行的操作是在幾分鍾內接收到輸入。 現在,我的腳本正在執行的操作是倒數5秒而不是1秒。 同樣,它也不會顯示我想要的分鍾。

我以邏輯方式制作了腳本,以登錄控制台並對其進行測試。 我在控制台中看到的是它每秒顯示一次,但是如果可以的話,它每秒顯示5秒。 這是jsbin: https ://jsbin.com/gigohajawo/3/edit ? js,consolehttps://jsbin.com/gigohajawo/3/edit ? js,console

這是代碼,任何幫助將不勝感激!!!

//makes sure the page is loaded first
$(document).ready(function() {
    //global variables
    //grabs text of an id and converts it to an int
    var countMin = 5;
    var count1 = 60;


    //when button id "but" is clicked...

       //while page is up, it keeps track each second that has passed
       for(; countMin >=0;countMin--){
            var counter1 = setInterval(function(){
                //calls timer function to count down
                 count1 = Timer(count1,counter1,countMin);
            },1000);
          count1 =60;
        }



    //counts down
    function Timer(count,counter,minutes){
        count--;
        //once it hits 0 seconds, the interval will stop counting
        if(count <=0){
            clearInterval(counter); 
            return count;
        }

        //displays the countdown
        if(minutes < 10){
            if(count < 10){
                console.log("0:0" + count);
            } else {
                console.log("0:" + count);
            }
        }else if(minutes > 0 && minutes < 10){
            if(count < 10){
                console.log("0" + minutes +":0" + count);
            } else {
               console.log("0"+minutes+":" + count);
            }
        } else{
            if(count < 10){
                console.log(minutes+":0" + count);
            } else {
               console.log=minutes+":" + count;
            }
        }

        return count;
    }

});

這個 JSBin似乎可以滿足您的要求。

編碼:

//makes sure the page is loaded first
$(document).ready(function() {
    //global variables
    //grabs text of an id and converts it to an int
    var count1 = 120;

    // Call a function every 1000 milliseconds (1 second)
    var counter1 = setInterval(function() {
      count1 = Timer(count1, counter1);
    }, 1000);


    //counts down
    function Timer(count,counter){
        // Decrement the seconds counter
        count--;

        // Get the minutes and seconds in whole numbers
        var minutes = Math.floor(count / 60);
        var seconds = count % 60;

        // Once it hits 0 seconds, the interval will stop counting
        if(count <=0){
            clearInterval(counter);     
        }

        // Pads the seconds with a 0
        if (seconds < 10) {
          seconds = "0" + seconds;
        }

        // Pads the minutes with a 0
        if (minutes < 10) {
          minutes = "0" + minutes;
        }

        //displays the countdown
        console.log(minutes + ":" + seconds)

        return count;
    }

});

請注意:

  1. 由於已將count1定義為全局變量,因此無需將其傳遞給Timer
  2. counter1

如果我要重寫它,我將執行以下操作:

//makes sure the page is loaded first
$(document).ready(function() {
    var timeInSeconds = 120;

    var timeCounter = setInterval(function() {
      timeInSeconds--;

      // If we hit 0 seconds clear the timer
      if (timeInSeconds <= 0) {
        clearInterval(timeCounter);
      }

      // Display the current time
      displayTime();
    }, 1000);


    function displayTime(){
        // Get the minutes and seconds in whole numbers
        var minutes = Math.floor(timeInSeconds / 60);
        var seconds = timeInSeconds % 60;

        // Pad with zeros using the Ternary operator
        seconds = (seconds < 10) ? "0" + seconds : seconds;
        minutes = (minutes < 10) ? "0" + minutes : minutes;

        // Display the countdown
        console.log(minutes + ":" + seconds)
    }

});

暫無
暫無

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

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