简体   繁体   中英

Refresh page when timer reaches 0 in JavaScript

<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} is the amount of seconds the counter start with. When it reaches 0 I want the page to reload. I tried :

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

but that didn't work..

Does this work for you?

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

To reload page, you have to submit a form.

<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 />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM