cookie相关js代码
function setCookie(n,v,mins,dn,path,time){ if(n){ if(!mins) mins = 365 * 24 * 60; if(!path) path = "/"; var date= new Date(); if(!time){ date.setTime(date.getTime()+(mins * 60 * 1000)); }else{ date.setTime(time+(mins * 60 * 1000)); } var expires = "; expires=" + date.toGMTString(); if(dn) dn = "domain=" + dn + "; "; document.cookie = n + "=" + v + expires + "; " + dn + "path=" + path; } } function cookiesave(n, v, mins, dn, path){ if(n){ if(!mins) mins = 365 * 24 * 60; if(!path) path = "/"; var date = new Date(); date.setTime(date.getTime()+(mins * 60 * 1000)); var expires = "; expires=" + date.toGMTString(); if(dn) dn = "domain=" + dn + "; "; document.cookie = n + "=" + v + expires + "; " + dn + "path=" + path; } } function cookieget(n){ var name = n + "="; var ca = document.cookie.split(';'); for(var i=0;i<ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(name) == 0) return c.substring(name.length,c.length); } return ""; }
调用相关代码 基于jq
$.post("goods_getCountDownTimeScope", {}, function (result) { var arr = result.split(','), nowTime = new Date(arr[0] * 1000),//当前时间 nowTime_yyyy = nowTime.getFullYear(), nowTime_MM = nowTime.getMonth() + 1, nowTime_dd = nowTime.getDate(), //当前开始和结束时间 sFuTime = nowTime_yyyy + "/" + nowTime_MM + "/" + nowTime_dd + " " + new Date(arr[1] * 1000).getHours() + ":" + new Date(arr[1] * 1000).getMinutes() + ":" + new Date(arr[1] * 1000).getSeconds(), sEndTime = nowTime_yyyy + "/" + nowTime_MM + "/" + nowTime_dd + " " + new Date(arr[2] * 1000).getHours() + ":" + new Date(arr[2] * 1000).getMinutes() + ":" + new Date(arr[2] * 1000).getSeconds(); var iCurTime = arr[0] * 1000, iBeginTime = new Date(sFuTime).getTime(), iEndTime = new Date(sEndTime).getTime(); var cookie_v = cookieget("fastSalesTip_box"); if (iCurTime >= iBeginTime && iCurTime <= iEndTime && (navigator.cookieEnabled || document.cookie)) { $('.fastSalesTip_box').show(); if (cookie_v == 'true' || (new Date().getTime() > iEndTime)) { $('.fastSalesTip_box').hide(); } ; } ; }, "text"); $('.fastSalesTip').find('.btn').click(function () {//关闭闪购 $.post("goods_getCountDownTimeScope", function (result) { var arr = result.split(','), nowTime = new Date(arr[0] * 1000),//当前时间 nowTime_yyyy = nowTime.getFullYear(), nowTime_MM = nowTime.getMonth() + 1, nowTime_dd = nowTime.getDate(), //当前开始和结束时间 sFuTime = nowTime_yyyy + "/" + nowTime_MM + "/" + nowTime_dd + " " + new Date(arr[1] * 1000).getHours() + ":" + new Date(arr[1] * 1000).getMinutes() + ":" + new Date(arr[1] * 1000).getSeconds(), sEndTime = nowTime_yyyy + "/" + nowTime_MM + "/" + nowTime_dd + " " + new Date(arr[2] * 1000).getHours() + ":" + new Date(arr[2] * 1000).getMinutes() + ":" + new Date(arr[2] * 1000).getSeconds(); var iCurTime = arr[0] * 1000, iBeginTime = new Date(sFuTime).getTime(), iEndTime = new Date(sEndTime).getTime(), diffTime = (iEndTime - iCurTime) / (60 * 1000); setCookie('fastSalesTip_box', 'true', diffTime, '', '', iCurTime); //console.log(nowTime+"||"+sFuTime+"||"+sEndTime); }, "text"); $(this).parents('.fastSalesTip_box').hide(); });
适用jquerycookie 插件
1、设置cookie过期时间为10秒
$('form').submit(function () { var date = new Date(); date.setTime(date.getTime() + 10000); console.log(date); $.cookie('name1', 'zhangsan', {expires: date}); $.cookie('name2', 'lisi'); console.log($.cookie('name1')) setInterval(function () { console.log($.cookie('name1')) }, 1000) return false; })
2、设置cookie过期时间为10天
$.cookie(‘name1′,’zhangsan’,{expires:10});
3、设置小时用setHours()和getHours方法 : date.setHours(date.getHours() + 1); 分钟设置类似,或者用毫秒的乘法