jQuery验证码发送时间秒递减(刷新存储cookie)

<input id="sendEmail" type="button" name="sendEmail" οnclick="show()" value="发送" style="cursor:pointer; width:218px;margin-left:3px;text-indent:0;text-align:center;margin-top:10px;"/>
function writeCookie(name, value, timeNum) {//把当前发送的时间写入cookie
        var expire = "";
        if (timeNum != null) {
            expire = new Date((new Date()).getTime() + timeNum);//设置有效时间段(当前时间+设置的有效时间长度)
            expire = "; expires=" + expire.toGMTString();//这里分号是为与前面的拼接需要"name=xxx;expires=yyy"
        }
        document.cookie = name + "=" + escape(value) + expire + ";path=/";
    }
    function readCookie(name) {//读取cookie中名称等于name的值
        var cookieValue = "";
        var search = name + "=";
        if (document.cookie.length > 0) {
            offset = document.cookie.indexOf(search);
            if (offset != -1) {
                offset += search.length;
                end = document.cookie.indexOf(";", offset);
                if (end == -1) end = document.cookie.length;
                cookieValue = (document.cookie.substring(offset, end))
            }
        }
        return cookieValue;
    }



    var tipId;
    function show() {
        writeCookie("daTime", (new Date()).getTime(), 60000)
        tipId = window.setInterval("start()", 1000);
    }
    $(function () {
        var tt = parseInt(((new Date()).getTime() - readCookie("daTime")) / 1000);
        if (tt < 60) {
            tipId = window.setInterval("start()", 1000);
            var vv = "(" + parseInt(60 - tt) + ")秒后重新发送";
            $("#sendEmail").attr("disabled", "disabled");
            $("#sendEmail").attr("value", vv);
        }
    })
    function start() {
        var tt = parseInt(((new Date()).getTime() - readCookie("daTime")) / 1000);
        if (tt < 60) {
            var vv = "(" + parseInt(60 - tt) + ")秒后重新发送";
            $("#sendEmail").attr("disabled", "disabled");
            $("#sendEmail").attr("value", vv);

        } else {
            $("#sendEmail").removeAttr("disabled");
            $("#sendEmail").attr("value", "发送");
            window.clearInterval(tipId);
        }
    }

  

转载于:https://www.cnblogs.com/boyzi/p/5333083.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值