gpt4 book ai didi

javascript - Node.js,多个计时器在完成时依次显示

转载 作者:行者123 更新时间:2023-12-03 02:45:20 26 4
gpt4 key购买 nike

大家好,我正在尝试创建多个计时器。当第一个计时器结束时,第二个计时器将开始,当第二个计时器结束时,第三个计时器将开始并结束,依此类推...我如何使用回调在 javascript 中实现它。我想在当前时间创建计时器,以便我在任何窗口上打开它都会显示相同的结果。非常感谢您的建议。

// 1st function start time 
var countDownDate = new Date("Jan 6, 2018 11:42:00").getTime(); //datetimefinal

// function2 end time
var countDownDate1 = new Date("Jan 6, 2018 11:42:25").getTime(); //date time final

//function z is called from function y
var z = function() {
alert('zzz');
}

// function y is called from function x
var y = function() {
// Get todays date and time
var now = new Date().getTime(); // from now date
// Find the distance between now an the count down date
var distance = countDownDate1 - now;

// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);

// Display the result in the element with id="demo"
document.getElementById("demo12").innerHTML = days + "d " + hours + "h " +
minutes + "m " + seconds + "s ";


if (distance < 0) {
// clearInterval(y);
document.getElementById("demo12").innerHTML = "EXPIRED";
var sz = new z(); //here function z is calling
}

}

// Update the count down every 1 second
var x = setInterval(function() {

// Get todays date and time
var now = new Date().getTime(); // from now date
// Find the distance between now an the count down date
var distance = countDownDate - now;

// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);

// Display the result in the element with id="demo"
document.getElementById("demo").innerHTML = days + "d " + hours + "h " +
minutes + "m " + seconds + "s ";

// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
var s = new y(); //here function y is calling
}
}, 1000);
<p id="demo"></p>
<p id="demo12"></p>
<p id="demo123"></p>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

最佳答案

您可以通过减少函数中的重复实现来改进代码,并且不在函数中使用全局变量(将函数需要的每个值传递给函数)。

const repeat = time => fn => {
const t = setInterval(
()=>fn(t)
,time
);
};
const getDistance = date1 => date2 => {
const diff = date1-date2;
return [
Math.floor(diff / (1000 * 60 * 60 * 24)),
Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)),
Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)),
Math.floor((diff % (1000 * 60)) / 1000),
diff
];
}
const setHtml = el=>html=>{
el.innerHTML = html;
};
const work = distanceFn => setHtmlFn => t => {
[days,hours,minutes,seconds,diff] = distanceFn(new Date().getTime());
if(diff<0){
setHtmlFn("EXPIRED");
clearInterval(t);//no longer call this function
}else{
setHtmlFn(`${days} d ${hours} h ${minutes} m ${seconds} s `);
}
}
repeat(1000)
(
work
(getDistance(new Date("Jan 6, 2018 11:42:25").getTime()))
(setHtml(document.getElementById("demo12")))
);
repeat(1000)
(
work
//3 days from now
(getDistance(new Date(new Date().getTime()+259200000).getTime()))
(setHtml(document.getElementById("demo")))
);
<p id="demo"></p>
<p id="demo12"></p>
<p id="demo123"></p>

关于javascript - Node.js,多个计时器在完成时依次显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48124937/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com