简体   繁体   English

Js setInterval不同段落不起作用

[英]Js setInterval for different paragraphs not working

I want to run a different timer for each paragraph. 我想为每个段落运行一个不同的计时器。 But unfortunatly it works according to the last paragraph. 但不幸的是,它根据最后一段起作用。 The function should run separatly for each paragraph. 该函数应针对每个段落分别运行。 Here is my code 这是我的代码

 $(".set_timer").each(function() { setTimer($(this).data("created_time"), $(this)); }); function setTimer(dateStart, ele) { countDownDate = new Date(dateStart).getTime(); x = setInterval(function() { // Get todays date and time now = new Date().getTime(); // Find the distance between now an the count down date distance = countDownDate - now; // Time calculations for days, hours, minutes and seconds days = Math.floor(distance / (1000 * 60 * 60 * 24)); hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); seconds = Math.floor((distance % (1000 * 60)) / 1000); // Output the result in an element with id="demo" ele.html(days + "d " + hours + "h " + minutes + "m " + seconds + "s "); // If the count down is over, write some text if (distance < 0) { clearInterval(x); ele.html("TIMER COMPLETED"); } }, 1000); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> <p class="set_timer" data-created_time="May 12, 2018 11:56:30"></p> <p class="set_timer" data-created_time="May 10, 2018 11:56:30"></p> <p class="set_timer" data-created_time="May 08, 2018 11:56:30"></p> <p class="set_timer" data-created_time="Apr 10, 2018 11:56:30"></p> <p class="set_timer" data-created_time="Jun 15, 2018 11:56:30"></p> <p class="set_timer" data-created_time="Jul 13, 2018 11:56:30"></p> 

Current Output: 电流输出:

459d 1h 15m 25s 459d 1h 15m 25s

459d 1h 15m 25s 459d 1h 15m 25s

459d 1h 15m 25s 459d 1h 15m 25s

459d 1h 15m 25s 459d 1h 15m 25s

459d 1h 15m 25s 459d 1h 15m 25s

It should be different according to the created_time data attribute 根据created_time data属性应有所不同

Its expected as all variables are defined in global scope. 由于所有变量都是在全局范围内定义的,因此可以预期。 Define then in local scope. 然后在本地范围内定义。

 $(".set_timer").each(function() { setTimer($(this).data("created_time"), $(this)); }); function setTimer(dateStart, ele) { let countDownDate = new Date(dateStart).getTime(); let x = setInterval(function() { // Get todays date and time let now = new Date().getTime(); // Find the distance between now an the count down date let distance = countDownDate - now; // Time calculations for days, hours, minutes and seconds let days = Math.floor(distance / (1000 * 60 * 60 * 24)); let hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); let minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); let seconds = Math.floor((distance % (1000 * 60)) / 1000); // Output the result in an element with id="demo" ele.html(days + "d " + hours + "h " + minutes + "m " + seconds + "s "); // If the count down is over, write some text if (distance < 0) { clearInterval(x); ele.html("TIMER COMPLETED"); } }, 1000); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> <p class="set_timer" data-created_time="May 12, 2018 11:56:30"></p> <p class="set_timer" data-created_time="May 10, 2018 11:56:30"></p> <p class="set_timer" data-created_time="May 08, 2018 11:56:30"></p> <p class="set_timer" data-created_time="Apr 10, 2018 11:56:30"></p> <p class="set_timer" data-created_time="Jun 15, 2018 11:56:30"></p> <p class="set_timer" data-created_time="Jul 13, 2018 11:56:30"></p> 

Just add a var to countDownDate = new Date(dateStart).getTime(); 只需添加varcountDownDate = new Date(dateStart).getTime();

so it will be 所以会的

 var countDownDate = new Date(dateStart).getTime();
 .
 .
 .
function setTimer(dateStart, ele) {
    countDownDate = new Date(dateStart).getTime();
    x = setInterval((function (countDownDate) {
            // Get todays date and time
           return function(){
                now = new Date().getTime();
                // Find the distance between now an the count down date
                distance = countDownDate - now;
                // Time calculations for days, hours, minutes and seconds
                days = Math.floor(distance / (1000 * 60 * 60 * 24));
                hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
                minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
                seconds = Math.floor((distance % (1000 * 60)) / 1000);

                // Output the result in an element with id="demo"
                ele.html(days + "d " + hours + "h " + minutes + "m " + seconds + "s ");
                // If the count down is over, write some text 
                if (distance < 0) {
                    clearInterval(x);
                    ele.html("TIMER COMPLETED");
                }
           }
        })(countDownDate), 1000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM