js cookie countdown page

Crazy friends wrote without saving
needs: the countdown page only once additional purchase beginning with the first
public method
cookie settings acquisition
function getCookie (c_name)
{

if (document.cookie.length>0)
{
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1)
    {
        c_start=c_start + c_name.length+1;
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;;
        return unescape(document.cookie.substring(c_start,c_end))
    }
}
return ""

}
function setCookie(cname,cvalue,exdays) {

var d = new Date();
d.setTime(d.getTime() + (exdays*60*1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";

}

Implementation steps
Click the plus purchase -> Click the current record time -> set a cookie
on the page, click:
function Payment () {

    var n = getCookie('now_time');
    if(!n){
        var nowtime = new Date();
        nowtime= nowtime.getTime();
        setCookie('now_time',nowtime,5);
    }
   
}

In the payment page call the method:
now: the current timestamp
function PaymentCountdown (now) {

  var now = now;
  var w = getCookie('now_time');
  if(w){
      var timeout =setInterval(function(){
          var dateTime = new Date();
          dateTime = dateTime.getTime();
          var diff = dateTime - now;
          var alltime = 5*60*1000;
          var c = alltime-diff;
          if(c>=0){
              var m = Math.floor(c/1000/60%60);
              var s = Math.floor(c/1000%60);
              var str =  "<span>"+m+"</span>:<span>"+s+"</span>";
              jQuery("#payment_time").html(str);
          }else if(c<0){
              clearInterval(timeout);
              jQuery("#payment_words").hide();
              jQuery("#payment_end").show();
          }
      }, 1000);

  }

};

Guess you like

Origin www.cnblogs.com/jlfw/p/11822939.html