<input type="text" id="times">
<input type="button" value="倒计时开始" onclick="count()">
<script type="text/javascript">
function counts(ti){
ti--;
}
function count(){
var tim=document.getElementById("times").value;
setInterval("counts(tim)",1000);
}
</script>
1,counts这个函数的作用是将当前value减1,这一步可以直接在count函数操作,所以可以不要。
2,count倒计时函数执行步骤:获取value,value值减1,将减一的结果赋值给value,每隔一秒执行一次该函数。这是倒计时的四步。
注意:减一和赋值可以同时进行,需要注意的是需要先减减,再赋值。还有就是需要进行判断,
第一个判断是如果倒计时已经执行了,就禁止点击按钮;
第二个判断是如果倒计时的value最后小于等于0的时候,始终赋值为0,并且用clearTimeout清除该倒计时。
function count(){
var tim=document.getElementById("times").value;
document.getElementById("times").value = --tim;
setTimeout(count(),1000);
}
<input type="text" id="times" value="10">
setInterval("counts(tim)",1000);
->
setInterval("counts(" + tim +")",1000);
<input type="text" id="times">
<input type="button" value="倒计时开始" onclick="count()">
<script type="text/javascript">
function counts() {
var tim = document.getElementById("times").value;
console.log(tim);
tim--;
document.getElementById("times").value=tim;
}
function count() {
setInterval(counts, 1000);
}
</script>
点击按钮出现60秒倒计时js代码,亲测可用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="js/jquery.js"></script>
</head>
<body>
<input type="button" id="btn" value="免费获取验证码" onclick="settime(this)" />
<script type="text/javascript">
var countdown=60;
function settime(val) {
if (countdown == 0) {
val.removeAttribute("disabled");
val.value="免费获取验证码";
countdown = 60;
} else {
val.setAttribute("disabled", true);
val.value="重新发送(" + countdown + ")";
countdown--;
}
setTimeout(function() {
settime(val)
},1000)
}
</script>
</body>
</html>
<input type="text" id="times" value="50">
<input type="button" value="倒计时开始" onclick="count()">
<script type="text/javascript">
function counts(ti){
ti--;
return ti;
}
function count(){
var tim=document.getElementById("times");
setInterval(function(){
timNum = counts(parseInt(tim.value));
tim.value = timNum;
},1000);
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
var tim = document.getElementById("times");
tim.value =60;
function counts() {
tim.value = tim.value -1;
}
function count(a) {
var time1 = setInterval("counts()", 1000);
a.setAttribute("onclick","count2()")
a.value = "重置";
}
function count2() {
tim.value =60;
}
少了给控件复制的语句