js cookie 页面倒计时

疯了啦 写了一篇没有保存
需求:页面倒计时 只从第一次加购开始
公共方法
cookie的设置 获取
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=/";

}

实现步骤
点击加购 -> 记录点击当前时间 ->设置个cookie
在点击页面:
function payment() {

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

在支付页面调用方法:
now:当前的时间戳
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);

  }

};

    原文作者:wxc1002
    原文地址: https://segmentfault.com/a/1190000016864154
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞