Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

javascript - 自动倒数计时器-Javascript(Auto Countdown Timer - Javascript)


I'm trying to make my timer automatically change to a second new Date() after the first countdown is expired .(我试图使我的计时器在第一次倒数计时到期后自动更改为第二个new Date() 。)


The purpose of this is in case I need to change date for the countdown, I won't have to manually edit and upload the js file after the it has expired.(这样做的目的是在需要更改倒计时日期的情况下,它不再需要手动编辑和上传js文件。) However I haven't managed to figure out how yet.(但是我还没有弄清楚怎么做。)

Below is the script that I've tried, output is "EXPIRED".(下面是我尝试过的脚本,输出为“ EXPIRED”。) Where did I do wrong?(我在哪里做错了?) Thanks for your support!(感谢您的支持!) <script> // Set the date we're counting down to var countDownDate = new Date("Jan 5, 2020 14:00:00").getTime(); var countDownDate2 = new Date("Jan 15, 2020 14:00:00").getTime(); // Update the count down every 1 second var x = setInterval(function() { // Get today's date and time var now = new Date().getTime(); // Find the distance between now and the count down date var distance = countDownDate - now; var distance2 = countDownDate2 - now; var a = distance; // Time calculations for days, hours, minutes and seconds var days = Math.floor(a / (1000 * 60 * 60 * 24)); var hours = Math.floor((a % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); var minutes = Math.floor((a % (1000 * 60 * 60)) / (1000 * 60)); var seconds = Math.floor((a % (1000 * 60)) / 1000); // Output the result in an element with id="demo" document.getElementById("demo").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s "; // Change to another exp. if (distance < 0 && distance2 >0) { function changeDate() { a = distance2; } } // If the count down is over, write some text else { clearInterval(x); document.getElementById("demo").innerHTML = "EXPIRED"; } }, 1000); </script>   ask by Jacob D. translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The first problem is that you look if distance is smaller than 1 and distance2 is bigger than one.(第一个问题是您要查看距离是否小于1,而距离2是否大于一个。)

But when distance is still active (which is the case at the time of writing) it will automatically go into the else branch.(但是,当距离仍然有效时(在撰写本文时就是这种情况),它将自动进入else分支。) So to fix this you need:(因此,要解决此问题,您需要:) // If the count down is over, write some text if (distance2 < 0) { clearInterval(x); document.getElementById("demo").innerHTML = "EXPIRED"; } EDIT:(编辑:) The second problem is that you change the variable after it is being printed.(第二个问题是您在打印变量后更改了变量。) So you have to go to the definition of a and change it from:(因此,您必须转到a的定义并将其更改为:) var a = distance; To:(至:) var a; if (distance < 0 && distance2 >0) { a = distance2; } else { a = distance; }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...