当JavaScript中的计时器达到0时刷新页面

时间:2010-12-22 03:18:04

标签: javascript timer reload counter

<form name="counter" ><input type="text" size="8" name="d2"></form> 

    <script> 
     var milisec=0 
     var seconds={$wait} 
     document.counter.d2.value='{$wait}' 

    function display(){ 
     if (milisec<=0){ 
        milisec=9 
        seconds-=1 
     } 
     if (seconds<=-1){ 
        milisec=0 
        seconds+=1 
     } 
     else 
        milisec-=1 
        document.counter.d2.value=seconds+"."+milisec 
        document.title=seconds+"."+milisec 
        setTimeout("display()",100) 
    } 

    if (document.counter.d2.value==0){
        location.reload(true)
    }

    display() 

    </script> <br />

{$time}是计数器开始的秒数。当它达到0时,我希望页面重新加载。我试过了:

 if (document.counter.d2.value==0){
     location.reload(true)
 }

但这不起作用..

2 个答案:

答案 0 :(得分:0)

这对你有用吗?

if (document.counter.d2.value==0){
    window.counter.submit();
}

要重新加载页面,您必须提交表单。

答案 1 :(得分:0)

<form name="counter" ><input type="text" size="8" name="d2"></form> 

<script> 
 var milisec=0 
 var seconds={$wait} 
 document.counter.d2.value='{$wait}' 

function display(){ 
 if (milisec<=0){ 
    milisec=9 
    seconds-=1 
 } 
 if (seconds<=-1){ 
    milisec=0 
    seconds+=1 
 } 
 else 
    milisec-=1 
    document.counter.d2.value=seconds+"."+milisec 
    document.title=seconds+"."+milisec 
    setTimeout("display()",100) 
}//<-mistake 

if (document.counter.d2.value=="0.0"){ //<- mistaked  
    location.reload(true)
}
} //<- must be here
display() 

</script> <br />