0

hello again,
Im tring to stop this redirect with a button. this is the orginal script.

<script language="JavaScript" type="text/javascript">  
    var count =16  
    var redirect="example.php"  

    function countDown(){  
     if (count <=0){  
      window.location = redirect;  
     }else{  
      count--;  
      document.getElementById("timer").innerHTML = ""+count+" seconds."  
      setTimeout("countDown()", 3000)  
     }  
    }  
        </script>  

    Browser refresh in     
    <span id="timer">  
    <script>  
     countDown(); 
    </script>  
    </span>  

and this is what i have been tring that doesnt work

<html>
<body>

<p id="count"></p>
<button onclick="myStopFunction()">Stop</button>   

<script language="JavaScript" type="text/javascript">  
    var count =16  
    var redirect="example.php"  

    function countDown(){  
     if (count <=0){  
      window.location = redirect;  
     }else{  
      count--;  
      document.getElementById("timer").innerHTML = ""+count+" seconds."  
      setTimeout("countDown()", 3000)  
     }  
    }  
function myStopFunction()
{
clearTimeout(count);
}
    </script>  

    Browser refresh in     
    <span id="timer">  
    <script>  
     countDown(); 
    </script>  
    </span>  
 </body>
</html>

any help with this would be great
Thank you

2
Contributors
2
Replies
5
Views
4 Years
Discussion Span
Last Post by Tko_1
0

You can use clearTimeout if you store the result of setTimeout in a variable, and pass that as parameter.

Your code is missing several semi-colons...

0

thanks pritaeas went a differant way, works perfect.
thanks again

<HTML>
<HEAD>
    <SCRIPT LANGUAGE="JavaScript">

    var time_left = 20;
    var cinterval;
    var timestatus=1;
    var redirect="example.php";

    function time_dec(){
        time_left--;
        document.getElementById('countdown').innerHTML = time_left;
        if(time_left <=0){
                window.location = redirect;
    }
    }

    function resumetime()
    {
        //time_left = 20;
        clearInterval(cinterval);
        cinterval = setInterval('time_dec()', 1000);
    }

    function defaultstart()
    {
        time_left = 20;
        clearInterval(cinterval);
        cinterval = setInterval('time_dec()', 1000);
    }

    function stopstarttime()
    {
        if(timestatus==1)
    {
        clearInterval(cinterval);
        document.getElementById('stopbutton').value="Start";
        timestatus=0;
    }
        else
    {
        clearInterval(cinterval);
        cinterval = setInterval('time_dec()', 1000);
        document.getElementById('stopbutton').value="Stop";
        timestatus=1;
    }

    }

    defaultstart();

    </SCRIPT>
</HEAD>
</HEAD>

<body>
    Redirecting In <span id="countdown">20</span>.
    <INPUT TYPE="button" value="stop" id="stopbutton" onclick="stopstarttime()">
</body>
</HTML>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.