JavaScript时钟倒计时

     我们在注册用户时会看到阅读遵守条款页面,下方有一个倒计时按钮,在倒计时未结束之前此button不可链接。或者有些页面在倒计时N秒后会跳转原来的页面,那么下面来看看用JavaScript实现的效果。面在倒计时N秒后会跳转原来的页面,那么下面来看看用JavaScript实现的倒计时效果。

       点击查看效果
      下面是倒计时自动跳转效果
      点击查看效果

     上代码:

<input type=”button” id=”agree”  onClick=”window.open(‘http://www.glwzu.com/?p=218′)” value=”(7)秒后自动关闭”>
<script type=”text/JavaScript“>//初始化关闭需要的时间

var settime=7;//定义自变量
var i;
var showthis;
document.all.agree.disabled=true;
for(i=1;i<=settime;i++){
  setTimeout(“update(“+i+”)”,i*1000) ;
}

function update(num){
  if(num==settime){
document.all.agree.value=”welcome to glwzu“;
document.all.agree.disabled=false;
}
else{
showthis=settime-num;
document.all.agree.value=”(“+showthis+”)秒后自动关闭”;
}
}
</script>

若要实现自动跳转,把“document.all.agree.value=”welcome to glwzu“”改成“location.href=”http://www.glwzu.com/?p=218“;”

这个很简单,for循环中变量“i”传递给参数“num”,给函数update(num)进行判断,如果时间到了,则显示button或者跳转,如果时间未到,则显示时间倒数。

其中 settime是关闭需要的时间,最后由settime-num即showthis来显示倒计时数。

您可能会喜欢:

4 Comments.

Leave a comment
  1. 呵呵,这个看到效果了,不错。

    [回复]

  2. @fly3q
    汉字显示还是有问题。

    [回复]

  3. Finding this post has solved my porblem

    [回复]

  4. So that’s the case? Quite a rvleaetion that is.

    [回复]

Leave a Reply

:laugh: :cool: :ding: :blaugh: :evil: :close: :han: :rp: more »
click to change 请输入验证码

( Ctrl + Enter )