admin管理员组文章数量:1335847
I Need to have a code for the timer that doesn't reset the countdown timer even you refresh the page.
I saw sample code at keith wood plug in, but don't know how to implement this in my html file. here's the link :
/countdown.html
I hope somebody could help me! thanks! :)
I just need Hour, Minutes and Seconds to show. And after the Time given, there is a prompt box that will show "Time is up! " . This one is for the online exam we are developing. Thank you so Much! :)
I Need to have a code for the timer that doesn't reset the countdown timer even you refresh the page.
I saw sample code at keith wood plug in, but don't know how to implement this in my html file. here's the link :
http://keith-wood.name/countdown.html
I hope somebody could help me! thanks! :)
I just need Hour, Minutes and Seconds to show. And after the Time given, there is a prompt box that will show "Time is up! " . This one is for the online exam we are developing. Thank you so Much! :)
Share Improve this question asked Nov 16, 2012 at 7:24 FunnyFalconFunnyFalcon 491 gold badge1 silver badge4 bronze badges 8- 1 There are implementation instructions at that link. Have you followed them? What errors have you encountered? – Abhilash Commented Nov 16, 2012 at 7:27
-
What you are looking for is in the
relative
ab on that link and looks like$('#until300s').countdown({until: +300});
– Abhilash Commented Nov 16, 2012 at 7:30 - Yes. I did what is instructed to that link. I included the links inside my head tag but I'm having trouble on setp 3 which is : "Connect the countdown functionality to your divs" please help on how to implement the keith wood plugin inside my html file. thanks! Abhilash :) – FunnyFalcon Commented Nov 16, 2012 at 7:31
- I downloaded the CSS and javascript files already. So far, my hhtml file contains: <head> <script type="text/javascript" src="ajax.googleapis./ajax/libs/jquery/1.6.1/…> <style type="text/css">@import "jquery.countdown.css";</style> <script type="text/javascript" src="jquery.countdown.js"></script> <script type="text/javascript" > $('#significantShort2').countdown({until: '+1h +1m +15s', format: 'YOWDHMS', significant: 2}); </script> </head> <body> </body> – FunnyFalcon Commented Nov 16, 2012 at 7:34
- 1 You may need to use cookies to store the end date of the question (the first time you visited the page + a certain amount of time). Keep in mind that javascript is not secure at all and anyone could "cheat" at your exam. You can for exemple add an extra verification in php in order to prevent that. – Bali Balo Commented Nov 16, 2012 at 7:46
3 Answers
Reset to default 1If you know that the user will have local cache enabled and able to store data, you could save 2 values to localStorage
, 1 for the currentTime
and 1 for the targetTime
. Then you pare the 2 in an interval and if currentTime > targetTime
, display your message.
Also bind to the onbeforeunload
event and save the new currentTime back into localStorage
. This will give you the persisted countdown you are seeking.
Here is an example of how you can do this:
var interval;
let minutes = 1;
let currentTime = localStorage.getItem('currentTime');
let targetTime = localStorage.getItem('targetTime');
if (targetTime == null && currentTime == null) {
currentTime = new Date();
targetTime = new Date(currentTime.getTime() + (minutes * 60000));
localStorage.setItem('currentTime', currentTime);
localStorage.setItem('targetTime', targetTime);
}
else{
currentTime = new Date(currentTime);
targetTime = new Date(targetTime);
}
if(!checkComplete()){
interval = setInterval(checkComplete, 1000);
}
function checkComplete() {
if (currentTime > targetTime) {
clearInterval(interval);
alert("Time is up");
} else {
currentTime = new Date();
document.write(
"\n <font color=\"white\"> Seconds Remaining:" + ((targetTime - currentTime) / 1000) + "</font>");
}
}
document.onbeforeunload = function(){
localStorage.setItem('currentTime', currentTime);
}
Note that I would've made a snippet however stackoverflow and other online IDE's are plaining about security breaches. Note that this is perfectly valid and does not breach any security. Save it as a .js
and run.
To start the "countdown" again, invoke localStorage.clear()
.
- Include the jQuery library in the head section of your page.
- Download and include the jQuery Countdown CSS and JavaScript in the head section of your page. Alternately, you can use the minimised version jquery.countdown.min.js (13.5K vs 31.4K, 4.4K when zipped).
- Connect the countdown functionality to your divs.
So, your final code will be:
<html>
<head>
<title>jQuery Countdown</title>
<style type="text/css">@import "jquery.countdown.css";</style>
<script type="text/javascript" src="http://ajax.googleapis./ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.countdown.js"></script>
<script type="text/javascript">
$(function () {
var year = new Date();
year = new Date(year.getFullYear(), 11 - 1, 20);
$('#dvCountDown').countdown({until: year, format: 'HMS'});
$('#year').text(austDay.getFullYear());
});
</script>
</head>
<body>
<h1>jQuery Countdown Basics</h1>
<p>Counting down to 20 November <span id="year">2012</span>.</p>
<div id="dvCountDown"></div>
</body>
</html>
Hope this helps!
If you want to use that script then you'll have to count by date and not a custom countdown value in order for it not to get reset on page refresh: http://jsfiddle/qWERa/1/
//Custom countdown value starting on page load
$('#CountdownbyValue').countdown({until: '+0h +0m +8s', format: 'HMS',onExpiry: liftOff,});
//Countdown by Date
$('#CountdownbyDate').countdown({until: new Date(2012, 11-1, 17, 10, 10, 10), format: 'HMS',onExpiry: liftOff,});
//Time is up dialog!
function liftOff() {
alert('Time is up!');
}
Full Code:
<html>
<head>
<title>Demo</title>
<script type='text/javascript' src='http://code.jquery./jquery-1.8.2.js'></script>
<script type='text/javascript' src='http://keith-wood.name/js/jquery.countdown.js'></script>
<link rel="stylesheet" type="text/css" href="http://keith-wood.name/css/jquery.countdown.css">
<script type='text/javascript'>
$(window).load(function(){
//Starts from time of page load
$('#CountdownbyValue').countdown({until: '+0h +0m +8s', format: 'HMS',onExpiry: liftOff,});
//Counts by Date
$('#CountdownbyDate').countdown({until: new Date(2012, 11-1, 17, 10, 10, 10), format: 'HMS',onExpiry: liftOff,});
//Time is up!
function liftOff() {
alert('Time is up!');
}
});
</script>
</head>
<body>
<div id="CountdownbyValue"></div>
<div id="CountdownbyDate"></div>
</body>
</html>
Your can also try:
window.onbeforeunload = function() {
return "Are you sure you want to quit this exam?";
}
I suggest you take a look at this Countdown timer with cookies
本文标签: javascriptCountdown Timer that doesn39t reset when you refresh the pageStack Overflow
版权声明:本文标题:javascript - Countdown Timer that doesn't reset when you refresh the page? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742367777a2461635.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论