Countdown Timer that doesn't reset when you refresh the page?

后端 未结 3 975
星月不相逢
星月不相逢 2020-12-30 16:47

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

相关标签:
3条回答
  • 2020-12-30 17:06

    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.net/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.com/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

    0 讨论(0)
  • 2020-12-30 17:07
    1. Include the jQuery library in the head section of your page.
    2. 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).
    3. 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.com/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!

    0 讨论(0)
  • 2020-12-30 17:21

    If 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 compare 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 complaining 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().

    0 讨论(0)
提交回复
热议问题