Comments (2)
Sure it is, you should save/update the cookie under the callback on any (or all) events. When you are initializing the script, instead of setting the date from a fixed date you should try to read the cookie.
var date = getCookie('time') || new Date();
You should never rely on dates generated from the client side, so would be a better approach if you generates the cookie on your backend (python, ruby, php), but depending on your specification it's ok.
The plugin can support this out-of-box.
from jquery.countdown.
this also works
/**
* Set cookie
* @param {string} name Name of cookie
* @param {string} value Value of the cookie
* @param {object} expires Date object of expire time
*/
function setCookie(name, value, expires) {
document.cookie = name + "=" + escape(value) + "; path=/" + ((expires == null) ? "" : "; expires=" + expires.toGMTString());
}
/**
* Get cookie
* @param {string} name Name of cookie
* @return {?string}
*/
function getCookie(name) {
var cname = name + "=";
var dc = document.cookie;
if (dc.length > 0) {
begin = dc.indexOf(cname);
if (begin != -1) {
begin += cname.length;
end = dc.indexOf(";", begin);
if (end == -1) end = dc.length;
return unescape(dc.substring(begin, end));
}
}
return null;
}
window.onload = function () {
function callback(event) {
// Update every second one time only
if(event.type != "seconds") return;
// Calculate the time left
var timeLeft = [
event.lasting.hours + event.lasting.days * 24,
event.lasting.minutes,
event.lasting.seconds
];
// Convert the number to two digits strings
for(var i = 0; i < timeLeft.length; ++i) {
timeLeft[i] = (timeLeft[i] < 10 ? '0' : '') + timeLeft[i].toString();
}
// Concatenate the strings with : and update the html
$(this).html(timeLeft.join(':'));
}
var oldTime = parseInt(getCookie('timertime'), 10);
if (0 < oldTime) {
timertime = oldTime;
} else {
timertime = new Date().getTime()+(1 * 24 * 60 * 60 * 1000);
}
$('div#clock').countdown( timertime, callback);
}
$(window).unload(function() {
// New Date object for the expire time
var exp = new Date();
// Set the expire time
exp.setTime(exp + 2592000000);
// Set a 'time' cookie with the current timer time and expire time object.
setCookie('timertime', timertime, exp);
});
from jquery.countdown.
Related Issues (20)
- Found Vulnerability - Need Update
- Web page css error
- How set start/end date? HOT 1
- Change Language
- Countdown as a progress bar HOT 1
- Timezone problem
- How to reset the counter when it reaches zero? HOT 1
- Count just business hours
- dutch language HOT 1
- Is epoch supported? HOT 1
- Redirect the page after count down 0
- vue
- Having trouble using woth foreach loop in php HOT 1
- Restart Time after completing Five Days
- Complicated callback..
- Days counter is not working properly HOT 1
- 'Uncaught TypeError: $(...).countdown is not a function' - may be correct!
- Countdown between business hours and excluding weekends and holidays
- Timer interval do not stop after finish.countdown
- Timer pause/resume or stop/start as well as remove are not working properly
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jquery.countdown.