I am dealing with the following puzzle and I cannot understand why it is happening.

我正在处理以下难题,我无法理解为什么会发生这种情况。

I have the following [I believe to be] equivalent pieces of javascript code, but one does not work as expected (notice the Console.Log):

我有以下[我相信]相当于javascript代码片段,但一个不能按预期工作(请注意Console.Log):

Updates the UI a single time, then unexpectantly stops updating : http://jsfiddle.net/silentwarrior/1m0v6oj1/

一次更新UI,然后无意中停止更新:http://jsfiddle.net/silentwarrior/1m0v6oj1/

    jQuery(function () {
    var isWorking = true;
    if (isWorking) {
        var timeEnd = 1431220406000; // generated from php
        var timeNow = 1431210557000; // generated from php
        var counter = 1;
        var t = "";
        setInterval(function () {
            try {
                var c = timeEnd - timeNow - counter;
                console.log(c);
                var d = new Date(c);
                if (c <= 1) {
                    window.location.href = window.location.href;
                    return;
                }
                t = "";
                if (d.getHours() > 0) {
                    t += d.getHours() + "h ";
                }
                if (d.getMinutes() > 0) {
                    t += d.getMinutes() + "m ";
                }
                t += d.getSeconds();
                jQuery("#factory_start_prod").val("Working ... " + t + "s left");
                counter = counter + 1;
            } catch (e) {

            }
        }, 1000);
    }
   });

Updates the UI constantly as expected: http://jsfiddle.net/silentwarrior/n3gkum2e/

按预期不断更新UI:http://jsfiddle.net/silentwarrior/n3gkum2e/

jQuery(function () {
    var isWorking = true;
    if (isWorking) {
        var timeEnd = 1431220406000;
        var timeNow = 1431210557000;
        var counter = 1;
        var t = "";
        setInterval(function () {
            try {
                var c = timeEnd - Date.now();
                console.log(c);
                var d = new Date(c);
                if (c <= 1) {
                    window.location.href = window.location.href;
                    return;
                }
                t = "";
                if (d.getHours() > 0) {
                    t += d.getHours() + "h ";
                }
                if (d.getMinutes() > 0) {
                    t += d.getMinutes() + "m ";
                }
                t += d.getSeconds();
                jQuery("#factory_start_prod").val("Working ... " + t + "s left");
                counter = counter + 1;
            } catch (e) {

            }
        }, 1000);
    }
});

The only difference from each other is that, the one that works uses Date.now() to get the current timestamp, while the other one uses a pre-built time stamp.

彼此唯一的区别是,工作的那个使用Date.now()来获取当前时间戳,而另一个使用预先构建的时间戳。

Why would one example update the text in the input correctly while the other wouldn't?

为什么一个示例会正确更新输入中的文本而另一个不能?

PS: it is important to me to use generated timestamps instead of Date.now() in order to not depend on the users system when calculating the time left.

PS:在计算剩余时间时,使用生成的时间戳而不是Date.now()对我来说非常重要,以便不依赖于用户系统。

1 个解决方案

#1


Your first example is working, however with each iteration you are only subtracting 1 from the timestamp value, which is equivalent to 1ms. Hence the value never appears to change unless you wait a really long time. You need to increment the counter by 1000 on each iteration for a second to be counted:

您的第一个示例正在运行,但是每次迭代时,您只从时间戳值中减去1,相当于1ms。因此,除非你等了很长时间,否则价值永远不会改变。您需要在每次迭代时将计数器增加1000,并计算一秒钟:

counter = counter + 1000;

Updated fiddle

更多相关文章

  1. 如何改变这个js的持续时间
  2. JQUERY中做表单验证,谁有带时间的日期选择控件?
  3. day049--jQuery文档操作示例
  4. web前端复习(二):js日期操作,实现时间显示和倒计时效果
  5. java前端实时绘制不连续时间点多系列折线图
  6. jQuery延迟淡入时间超过预期
  7. HTML+jQuery图片上传示例
  8. 为什么所有验证都在同一时间进行
  9. HTML显示日期时间代码 - [js 特效代码]

随机推荐

  1. Android韩国市场占有率超过95%
  2. 如何解决:Android中 Error generating fin
  3. Android DVM
  4. Android的NDK开发
  5. Android(安卓)动画整理(3.0以上)
  6. 丢失Android系统库或者Conversion to Dal
  7. WebView---Android与js交互实例
  8. Android4.2增加新键值
  9. android与js的交互
  10. android静默安装的实现(turn)