随机生成验证码(带计时器)

来源:互联网 发布:tp-link路由器网络设置 编辑:程序博客网 时间:2024/06/10 22:40

//注意事项:多次调用后台action时,如果传的参数相同,action只会执行一次,
//所以为了每次刷新验证码,并将后台生成的验证码传到前台,ajax的参数必须是变化的。
function changeIdentifyCode() {
var timeOut;
var nextTime;
timeOut = document.getElementById("lb").innerText;
timeOut = timeOut - 1;
// 当倒计时为0时重新生成验证码,重新开始倒计时
if(timeOut == 0) {
// 超时后后台的处理
new Ajax.Request(contactUrl("访问的url")), {
Parameters:(变量:值)
},
method:"get",
asynchronous:false,
onSuccess:function(transport) {
var obj = transport.responseJSON;
if(obj != null) {
// 刷新验证码
document.getElementById("identifyCode").innerTest=obj.identifyCode;
}
}
//重新计时
timeOut = document.getElementById("timeOut").innerText;
}

document.getElementById("lb").innerText = timeOut;
setTimeout("changeIdentifyCode()",1000);
}




<body>
<s:hidden value="timeOut">
验证码:<s:property key="identifyCode">
倒计时:<label id="lb"><s:property key="timeOut"></label>
</body>
原创粉丝点击