jquery--cookie应用
示例:发送手机验证码
防止页面刷新后,发送验证码按钮重置
注:橙色部分为后增加代码,为防止验证码等待期间用户退出或者切换到其他页面以至于很久之后回到当前页面倒计时还在的问题,加入时间对比,记录用户发送验证码时+60秒后的时间,下次进入判断当前时间是否大于记录时间,如果大于或者不存在,则距离上次发送验证码已超过60秒,这时不再读取倒计时。
1.将发送验证码放入cookie $.cookie("time", time); 2.当页面载入时判断cookie是否为空
var date = $.cookie("date");//取上次发送验证码时记录的时间 (发送时间+60s)
if(date && (new Date().getTime() < new Date(date).getTime()) ){//判断当前时间是否小于记录时间
if ($.cookie("time") != undefined && $.cookie("time") != 'NaN'
&& $.cookie("time") != 'null') {// cookie存在倒计时
timekeeping();//修改发送验证码按钮方法
} else {// cookie 没有倒计时
$('#ihd_getcode_btn').prop("disabled", false);
_button.removeClass("disabled");
}
}
3.时间未到时进入修改发送验证码按钮方法
function timekeeping() {
// 把按钮设置为不可以点击
_button.prop("disabled", true);
_button.addClass("disabled");
// 从cookie 中读取剩余倒计时
var time = $.cookie("time");
var interval = setInterval(function() {// 每秒读取一次cookie
// 把剩余总倒计时减掉1
time--;
if (time > 0) {// 剩余倒计时不为零
// 在发送按钮显示剩余倒计时
_button.html(time + "秒后重发");
// 重新写入总倒计时
$.cookie("time", time,{ path: '/',expires : (1/86400)*time});
} else {// 剩余倒计时为零,则显示 重新发送,可点击
// 清除定时器
clearInterval(interval);
// 删除cookie
time = $.cookie("time", time, {expires : -1});
_button.prop("disabled", false);
_button.removeClass("disabled");
_button.html("获取验证码");
}
}, 1000);
};
注:发送验证码方法:
$(".ihd_getcode_btn").click(function() {
var phone = $('input[name="phone"]').val();
if (!mobileRegex.test(phone)) {
apus.ui.toastr.error("手机号格式错误");
return;
}
// 发送验证码
_button.prop("disabled", true);
_button.addClass("disabled");
apus.ui.maskLlock(true);
$.ajax({
type : "post",
url : getUrl('/vip/sendCode'),
dataType : "json",
data : {
phone : phone
},
success : function(result) {
apus.ui.maskLlock(false);
if (result.success) {
apus.ui.toastr.info("验证码发送成功");
$.cookie("time", 60,{ path: '/',expires : (1/86400)*60});
var curDate = new Date();
curDate.setSeconds(curDate.getSeconds()+60);
$.cookie("date",curDate,{ path: '/',expires : (1/86400)*60});
timekeeping();
} else {
apus.ui.toastr.error("验证码发送失败," + result.msg);
_button.prop("disabled", false);
_button.removeClass("disabled");
_button.html("获取验证码");
}
},
error : function(e) {
apus.ui.maskLlock(false);
_button.prop("disabled", false);
_button.removeClass("disabled");
_button.html("获取验证码");
apus.ui.toastr.error("网络异常,请稍候重试");
}
});
});
jquery--cookie应用的更多相关文章
- jquery.cookie的使用
今天想到了要为自己的影像日记增加赞的功能,并且需要用到cookie. 记得原生的js操作cookie也不是很麻烦的,但似乎jquery更简单,不过相比原生js,需要额外引入2个文件,似乎又不是很好,但 ...
- jQuery cookie使用
什么是jquery cookie? A simple, lightweight jQuery plugin for reading, writing and deleting cookies. Usa ...
- jQuery插件 -- Cookie插件jquery.cookie.js(转)
Cookie是网站设计者放置在客户端的小文本文件.Cookie能为用户提供很多的使得,例如购物网站存储用户曾经浏览过的产品列表,或者门户网站记住用户喜欢选择浏览哪类新闻. 在用户允许的情况下,还可以存 ...
- jquery cookie的用法
http://www.cnblogs.com/qiantuwuliang/archive/2009/07/19/1526663.html jQuery cookie是个很好的cookie插件,大概的使 ...
- jquery.cookie.js && java后台代码 操作cookie实现记住当前用户输入信息代码
下载jquery.cookie.js地址看这里:http://pan.baidu.com/s/1gdCPaN5 //初始化页面时验证是否记住了密码 $(document).ready(function ...
- 【转】jquery.cookie.js的使用
Cookie是由服务器端生成,发送给User-Agent(一般是浏览器),浏览器会将Cookie的key/value保存到某个目录下的文本文件内,下次请求同一网站时就发送该Cookie给服务器(前提是 ...
- jquery.cookie.js 操作cookie实现记住密码功能的实现代码
jquery.cookie.js操作cookie实现记住密码功能,很简单很强大,喜欢的朋友可以参考下. 复制代码代码如下: //初始化页面时验证是否记住了密码 $(document).ready( ...
- jquery.cookie() 的使用(原)
jquery.cookie()是一个轻量级的cookie 插件,可以读取.写入.删除 cookie. 步奏: 1. 添加jQuery插件和jQuery.cookie插件 <script src= ...
- Jquery.cookie.js 源码和使用方法
jquery.cookie.js源码和使用方法 jQuery操作cookie的插件,大概的使用方法如下 $.cookie(‘the_cookie’); //读取Cookie值$.cookie(’the ...
- jQuery.cookie.js插件了解及使用方法
jquery.cookie.js插件实现浏览器的cookie存储,该插件是基于jquery开发,方便cookie使用. jquerycookie.js的下载地址 http://plugins.jque ...
随机推荐
- this调用属性
示例: class Person{ private String name; private int age; public Person(String name,int age){ this.nam ...
- 【旧文章搬运】Windows句柄表分配算法分析(二)
原文发表于百度空间,2009-03-30========================================================================== 四.句柄表 ...
- VS2010中编写x64汇编的具体方法
编写涉及系统特性的一些底层程序,特别是ShellCode,不可避免地要采用直接编写汇编代码的方式. 在目标平台为x86模式时,可以直接使用内联汇编,这个很多人都比较熟悉了,也非常地方便. 但是当目标平 ...
- 【推荐】 体验SubSonic
SubSonic简介 SubSonic配置 利用sonic.exe来生成代码 通过Substage来生成代码 简单操作示例 1.SubSonic简介 一句讲完就是:SubSonic就是一个ORM开源框 ...
- 在Emacs下用C/C++编程(转载)
转自:http://www.caole.net/diary/emacs_write_cpp.html Table of Contents 版权说明和参考文献 参考文献: 版权说明: 序 基本流程 基本 ...
- Thrift 使用TNonblockingServer模型时调用PosixThreadFactory出错。
Thrift 使用TNonblockingServer模型时调用PosixThreadFactory出错. 我定位到shared_ptr<PosixThreadFactory> thr ...
- maven groupid与artifactid
groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找. groupId一般分为多个段 ...
- SQL-添加字段处理
1.alter table [dbo].[SiteTracks] drop constraint DF__SiteTrack__Audit__47DBAE452.ALTER TAB ...
- php.ini配置文件位置
laravel之今天遇到个意想不到的问题: 我在测试文件上传,大于2M的文件时候hasFile() 方法报错,这一定是文件大小限制.接下来就跳坑了 1.首先查找php.ini的位置,就用find / ...
- POM报错Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2.6 from
解决方式一: 1.查看.m2\repository\org\apache\maven\plugins\maven-resources-plugin\下maven-resources-plugin- ...