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
190 views
in Technique[技术] by (71.8m points)

javascript - JQuery countdown after refresh

Hi guys I've created a jQuery countdown but I want it to stay counting even if I refresh the page, here it is the source code:

function JBCountDown(settings) {
var glob = settings;

function deg(deg) {
    return (Math.PI/180)*deg - (Math.PI/180)*90
}

glob.total   = Math.floor((glob.endDate - glob.startDate)/86400);
glob.days    = Math.floor((glob.endDate - glob.now ) / 5000);
glob.hours   = 24 - Math.floor(((glob.endDate - glob.now) % 86400) / 3600);
glob.minutes = 60 - Math.floor((((glob.endDate - glob.now) % 86400) % 3600) / 60) ;

if (glob.now >= glob.endDate) {
    return;
}

var clock = {
    set: {
        days: function(){
            var cdays = $("#canvas_days").get(0);
            var ctx = cdays.getContext("2d");
            ctx.clearRect(0, 0, cdays.width, cdays.height);
            ctx.beginPath();
            ctx.strokeStyle = glob.daysColor;

            ctx.shadowBlur    = 10;
            ctx.shadowOffsetX = 0;
            ctx.shadowOffsetY = 0;
            ctx.shadowColor = glob.daysGlow;

            ctx.arc(94,94,85, deg(0), deg((360/glob.total)*(glob.total - glob.days)));
            ctx.lineWidth = 17;
            ctx.stroke();
            $(".clock_days .val").text(glob.days);
        },

        hours: function(){
            var cHr = $("#canvas_hours").get(0);
            var ctx = cHr.getContext("2d");
            ctx.clearRect(0, 0, cHr.width, cHr.height);
            ctx.beginPath();
            ctx.strokeStyle = glob.hoursColor;

            ctx.shadowBlur    = 10;
            ctx.shadowOffsetX = 0;
            ctx.shadowOffsetY = 0;
            ctx.shadowColor = glob.hoursGlow;

            ctx.arc(94,94,85, deg(0), deg(15*glob.hours));
            ctx.lineWidth = 17;
            ctx.stroke();
            $(".clock_hours .val").text(24 - glob.hours);
        },

        minutes : function(){
            var cMin = $("#canvas_minutes").get(0);
            var ctx = cMin.getContext("2d");
            ctx.clearRect(0, 0, cMin.width, cMin.height);
            ctx.beginPath();
            ctx.strokeStyle = glob.minutesColor;

            ctx.shadowBlur    = 10;
            ctx.shadowOffsetX = 0;
            ctx.shadowOffsetY = 0;
            ctx.shadowColor = glob.minutesGlow;

            ctx.arc(94,94,85, deg(0), deg(6*glob.minutes));
            ctx.lineWidth = 17;
            ctx.stroke();
            $(".clock_minutes .val").text(60 - glob.minutes);
        },
        seconds: function(){
            var cSec = $("#canvas_seconds").get(0);
            var ctx = cSec.getContext("2d");
            ctx.clearRect(0, 0, cSec.width, cSec.height);
            ctx.beginPath();
            ctx.strokeStyle = glob.secondsColor;

            ctx.shadowBlur    = 10;
            ctx.shadowOffsetX = 0;
            ctx.shadowOffsetY = 0;
            ctx.shadowColor = glob.secondsGlow;

            ctx.arc(94,94,85, deg(0), deg(6*glob.seconds));
            ctx.lineWidth = 17;
            ctx.stroke();

            $(".clock_seconds .val").text(60 - glob.seconds);
        }
    },

    start: function(){
        /* Seconds */
        var cdown = setInterval(function(){
            if ( glob.seconds > 59 ) {
                if (60 - glob.minutes == 0 && 24 - glob.hours == 0 && glob.days == 0) {
                    clearInterval(cdown);

                    /* Countdown is complete */

                    return;
                }
                glob.seconds = 1;
                if (glob.minutes > 59) {
                    glob.minutes = 1;
                    clock.set.minutes();
                    if (glob.hours > 23) {
                        glob.hours = 1;
                        if (glob.days > 0) {
                            glob.days--;
                            clock.set.days();
                        }
                    } else {
                        glob.hours++;
                    }
                    clock.set.hours();
                } else {
                    glob.minutes++;
                }
                clock.set.minutes();
            } else {
                glob.seconds++;
            }
            clock.set.seconds();
        },1000);
    }
}
clock.set.seconds();
clock.set.minutes();
clock.set.hours();
clock.set.days();
clock.start();

}

I think that I need to create a cookie file and take the data from it, but as I'm a beginner I don't know how to do it, so please if someone could help me, thank you a lot.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Hey the reason the time/date is resetting every time you refresh the page is because the current time i.e glob.now is not dynamic so i created new variables for

glob.startDate = sDate
glob.endDate = eDate
glob.now = nDate

i.e Also note that the date is in milliseconds from 1970 (check this out if you dont get this http://www.w3schools.com/jsref/jsref_gettime.asp)

        function JBCountDown(settings) {
        var glob = settings;
       // var glob.startDate = 18316800;
       //  var glob.endDate = 26179200;
        var nDate = (new Date().getTime())/1000;
        var eDate = ((new Date(2013, 9, 31, 09, 0, 0, 0)).getTime())/1000; 
        var sDate = ((new Date(2013, 7, 1, 09, 0, 0, 0)).getTime())/1000; 
        function deg(deg) {
            return (Math.PI/180)*deg - (Math.PI/180)*90
        }


        glob.total   = Math.floor((eDate - sDate)/86400);
        glob.days    = Math.floor((eDate - nDate ) / 86400);
        glob.hours   = 24 - Math.floor(((eDate - nDate) % 86400) / 3600);
        glob.minutes = 60 - Math.floor((((eDate - nDate) % 86400) % 3600) / 60) ;

        alert (eDate - sDate);
        if (nDate >= eDate) {
            return;
        }

        var clock = {
            set: {
                days: function(){
                    var cdays = $("#canvas_days").get(0);
                    var ctx = cdays.getContext("2d");
                    ctx.clearRect(0, 0, cdays.width, cdays.height);
                    ctx.beginPath();
                    ctx.strokeStyle = glob.daysColor;

                    ctx.shadowBlur    = 10;
                    ctx.shadowOffsetX = 0;
                    ctx.shadowOffsetY = 0;
                    ctx.shadowColor = glob.daysGlow;

                    ctx.arc(94,94,85, deg(0), deg((360/glob.total)*(glob.total - glob.days)));
                    ctx.lineWidth = 17;
                    ctx.stroke();
                    $(".clock_days .val").text(glob.days);
                },

                hours: function(){
                    var cHr = $("#canvas_hours").get(0);
                    var ctx = cHr.getContext("2d");
                    ctx.clearRect(0, 0, cHr.width, cHr.height);
                    ctx.beginPath();
                    ctx.strokeStyle = glob.hoursColor;

                    ctx.shadowBlur    = 10;
                    ctx.shadowOffsetX = 0;
                    ctx.shadowOffsetY = 0;
                    ctx.shadowColor = glob.hoursGlow;

                    ctx.arc(94,94,85, deg(0), deg(15*glob.hours));
                    ctx.lineWidth = 17;
                    ctx.stroke();
                    $(".clock_hours .val").text(24 - glob.hours);
                },

                minutes : function(){
                    var cMin = $("#canvas_minutes").get(0);
                    var ctx = cMin.getContext("2d");
                    ctx.clearRect(0, 0, cMin.width, cMin.height);
                    ctx.beginPath();
                    ctx.strokeStyle = glob.minutesColor;

                    ctx.shadowBlur    = 10;
                    ctx.shadowOffsetX = 0;
                    ctx.shadowOffsetY = 0;
                    ctx.shadowColor = glob.minutesGlow;

                    ctx.arc(94,94,85, deg(0), deg(6*glob.minutes));
                    ctx.lineWidth = 17;
                    ctx.stroke();
                    $(".clock_minutes .val").text(60 - glob.minutes);
                },
                seconds: function(){
                    var cSec = $("#canvas_seconds").get(0);
                    var ctx = cSec.getContext("2d");
                    ctx.clearRect(0, 0, cSec.width, cSec.height);
                    ctx.beginPath();
                    ctx.strokeStyle = glob.secondsColor;

                    ctx.shadowBlur    = 10;
                    ctx.shadowOffsetX = 0;
                    ctx.shadowOffsetY = 0;
                    ctx.shadowColor = glob.secondsGlow;

                    ctx.arc(94,94,85, deg(0), deg(6*glob.seconds));
                    ctx.lineWidth = 17;
                    ctx.stroke();

                    $(".clock_seconds .val").text(60 - glob.seconds);
                }
            },

            start: function(){
                /* Seconds */
                var cdown = setInterval(function(){
                    if ( glob.seconds > 59 ) {
                        if (60 - glob.minutes == 0 && 24 - glob.hours == 0 && glob.days == 0) {
                            clearInterval(cdown);

                            /* Countdown is complete */

                            return;
                        }
                        glob.seconds = 1;
                        if (glob.minutes > 59) {
                            glob.minutes = 1;
                            clock.set.minutes();
                            if (glob.hours > 23) {
                                glob.hours = 1;
                                if (glob.days > 0) {
                                    glob.days--;
                                    clock.set.days();
                                }
                            } else {
                                glob.hours++;
                            }
                            clock.set.hours();
                        } else {
                            glob.minutes++;
                        }
                        clock.set.minutes();
                    } else {
                        glob.seconds++;
                    }
                    clock.set.seconds();
                },1000);
            }
        }
        clock.set.seconds();
        clock.set.minutes();
        clock.set.hours();
        clock.set.days();
        clock.start();
    }

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

...