Div文本不更新javascript代码

div text is not updating on javascript code

本文关键字:javascript 代码 更新 文本 Div      更新时间:2023-09-26
var now = new Date().getTime();
var remainingSeconds = (new Date(new Date(new Date(now + 3600000).setMinutes(1)).setSeconds(0)) - now) / 1000;
console.log(remainingSeconds);
if (remainingSeconds>=120) {
    $('#countdown').text('120');
}else{
    $('#countdown').text(remainingSeconds);
}

我使用这个代码来计算从现在到下一个小时的第一分钟之间有多少秒。关键是,如果remainingSeconds小于120秒,div文本不会改变。脚本已经在文档中准备好了,因为我只在页面加载时检查它。我为此创建了一个小提琴

问题是,在您的代码中remainingSeconds永远不会低于60。这是一个更新的版本,包括一个变通/解决方案。

$(document).ready(function(){
    var now = new Date().getTime();
    var remainingSeconds = (new Date(new Date(new Date(now + 3600000).setMinutes(1)).setSeconds(0)) - now) / 1000;
    // --- Workaround-Begin ---
    if ( remainingSeconds >= 3600 ) {
        remainingSeconds -= 3600;
    }
    // --- Workaround-End ---
    console.log(remainingSeconds);
    if (remainingSeconds>=120) {
        $('#countdown').text('120');
    }else{
        $('#countdown').text(remainingSeconds);
    }
});