Code Monkey home page Code Monkey logo

Comments (2)

hilios avatar hilios commented on July 16, 2024

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.

 avatar commented on July 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.