I have jquery countdown which I took from here and tried to modify with cookie to get persistent result as following:
Edited
<script type="text/javascript">
(function($) {
function countdown(el, options) {
var calc = function (target, current) {
var o = {};
var datetarget = getCookie('Target');
if(!datetarget) {
clearInterval(cd.id);
var datetarget = target.getTime()/1000;
}
if(datetarget <= 0) { return true; }
deleteCookie('Target');
o.seconds = datetarget;
o.seconds %= 86400;
o.hours = Math.floor(o.seconds/3600);
o.seconds -= o.hours * (3600);
o.minutes = Math.floor(o.seconds/60);
o.seconds -= o.minutes * (60);
o.seconds %= 60;
datetarget -= 1;
setCookie('Target',datetarget,1);
return o;
};
It somehow worked but i still got float instead of integer on “seconds” part and wrong result on “hours”. Any ideas?
You never rounded it. While the milliseconds you got from
getTimewere an integer, you got a float with 3 decimals after dividing by 1000 to convert to seconds.Also, the EcmaScript Modulus operator does work on floats, instead of truncating operands and result to integers. To cite the spec:
That last part also explains why we likely get some other decimals from small rounding errors in the floating point arithmetic.
You may omit one of the equivalent lines
and add one of
I can’t see that. Might it be possible that you did expect the value behave according your timezone?
getTimereturns milliseconds since epoch, which is in UTC.