javascript - 倒计时脚本,无法正确显示或倒计时

标签 javascript html function for-loop countdown

所以我一直在努力调试我的番茄时钟脚本。我想要这个脚本做的是它将接收输入(在几分钟内)。现在我的脚本正在做的是倒计时 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;
    }

});

最佳答案

This 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)
    }

});

关于javascript - 倒计时脚本,无法正确显示或倒计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34459256/

相关文章:

javascript - Safari 5.1.4 showModalDialog 返回未定义

javascript - 将 MySQL 数据库中的 UTC 日期时间转换为客户端时区,无需任何库

javascript - 如何访问模块外部的 Angular `value`?

javascript - fileReader.readAsText() JavaScript : result not displayed

html - 在 HTML 代码中使用大量换行符有什么好处吗?

html - 在设置网页样式时是否必须使用位置属性?

javascript - Skrollr 视差不适用于 iOS 和 Android 设备

c - (C) 函数之间和函数内——指针

c++ - 在程序的函数中创建对象数组

c - 为什么在函数调用中找不到指针数组的大小?