倒数计时器不起作用

时间:2018-11-28 10:18:49

标签: javascript countdowntimer

我正在尝试运行此倒数计时器,但无法正常工作,请帮助我 我尝试在两个数字之间生成一个随机数并启动计时器,但是它可以正常工作

  String.prototype.toHHMMSS = function () {
     var sec_num = parseInt(this, 10); // don't forget the second parm
     var hours = Math.floor(sec_num / 3600);
     var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
     var seconds = sec_num - (hours * 3600) - (minutes * 60);

    if (hours < 10) {
        hours = "0" + hours;
    }
    if (minutes < 10) {
        minutes = "0" + minutes;
    }
    if (seconds < 10) {
        seconds = "0" + seconds;
    }
    var time = hours + ':' + minutes + ':' + seconds;
    return time;
}

function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}

var count = getRandomArbitrary(21000,23000);

var counter = setInterval(timer, 1000);

function timer() {


    console.log(count);

    if (parseInt(count) <= 0) {
        clearInterval(counter);
        return;
    }
    var temp = count.toHHMMSS();
    count = (parseInt(count) - 1).toString();

    $('#timer').html(temp);
}

HTML代码

<div id="timer"></div>

2 个答案:

答案 0 :(得分:0)

您已经使用转换函数扩展了字符串原型,但是您没有使用字符串(getRandom...toHHMMSS = function (str) { var sec_num = parseInt(str, 10); // don't forget the second parm var hours = Math.floor(sec_num / 3600); var minutes = Math.floor((sec_num - (hours * 3600)) / 60); var seconds = sec_num - (hours * 3600) - (minutes * 60); if (hours < 10) { hours = "0" + hours; } if (minutes < 10) { minutes = "0" + minutes; } if (seconds < 10) { seconds = "0" + seconds; } var time = hours + ':' + minutes + ':' + seconds; return time; } function getRandomArbitrary(min, max) { return Math.random() * (max - min) + min; } var count = getRandomArbitrary(21000,23000); var counter = setInterval(timer, 1000); function timer() { console.log(count); if (parseInt(count) <= 0) { clearInterval(counter); return; } var temp = toHHMMSS(count); count = (parseInt(count) - 1).toString(); $('#timer').html(temp); }方法中的数字)。请改用普通函数,它将在更多类型上起作用:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="timer"></div>
{{1}}

答案 1 :(得分:0)

尝试我的代码。

您的代码问题是调用时没有将count.toHHMMSS();这一行转换为字符串。

String.prototype.toHHMMSS = function () {
     var sec_num = parseInt(this, 10); // don't forget the second parm
     var hours = Math.floor(sec_num / 3600);
     var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
     var seconds = sec_num - (hours * 3600) - (minutes * 60);

    if (hours < 10) {
        hours = "0" + hours;
    }
    if (minutes < 10) {
        minutes = "0" + minutes;
    }
    if (seconds < 10) {
        seconds = "0" + seconds;
    }
    var time = hours + ':' + minutes + ':' + seconds;
    return time;
}

function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}

var count = getRandomArbitrary(21000,23000);

var counter = setInterval(timer, 1000);

function timer() {


    console.log(count);

    if (parseInt(count) <= 0) {
        clearInterval(counter);
        return;
    }
    var temp = count.toString().toHHMMSS();
    count = (parseInt(count) - 1).toString();

    $('#timer').html(temp);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>
<div id="timer"></div>