时间变量/函数未在JS

时间:2015-06-21 21:28:57

标签: javascript jquery

(我正在使用jQuery,bootstrap和bootbox,因为JSFiddle没有显示console.log)

所以我正在尝试console.log当前的第二个,但它只是记录页面加载的第二个。我将它从一个变量更改为一个函数,看看它是否会有所帮助,但它仍然会做同样的事情。

JSFiddle:

$(document).ready(function () {
    var d = new Date();
    // I changed this from a variable that was just
    // var secs = d.getSeconds();
    // Thinking that it was saving the second in which it loaded
    // As the second, but that still didn't help.
    var secs = function () {
        return d.getSeconds();
    };
    setInterval(function(){
        // This bootbox.alert is like a fancy console.log
        bootbox.alert(secs().toString());
        // Change the 100000 to 1000 when you want to run the code, and change it again when you want to edit it as It'll keep spamming you.
    }, 100000);
});

1 个答案:

答案 0 :(得分:0)

每次要更新秒数时,都需要调用new Date()

将您的secs功能更改为以下内容并删除d变量。

var secs = function () {
    return new Date().getSeconds();
};

每次拨打secs();

时,此更改都会查找当前时间

您的实施引用了相同的时间点variable d,这就是秒数似乎没有变化的原因。

See my update to your fiddle