Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Sep 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool stopping a setTimeout()?

    I can set a function to execute at an interval fine, but unfortunately the tutorial I was looking at did not include how to stop the function from executing! Can someone please help me out here?

    Thank you for at least reading my post.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    clearTimeout(variable_setTimeout)


    var i=0;
    var delay = 2000;//milliseconds
    function testC(){
    alert(i);i++
    if(i==2){clearTimeout(setT)}
    else{var setT = setTimeout('test()',delay)}
    }

    http://www.w3schools.com/js/js_timing.asp
    Last edited by Kor; 10-18-2005 at 04:07 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Regular Coder
    Join Date
    May 2002
    Location
    Helsinki, Finland
    Posts
    231
    Thanks
    0
    Thanked 1 Time in 1 Post
    I think more proper way of clearing timeout should be done like this:
    Code:
    function doSomething()
    {
    	// Something you're doing
    	// besides clearing timeout
    	
    	try
    	{
    		clearTimeout(toObj);
    	}
    	catch(e)
    	{
    		// Error handling
    	}
    }
    
    
    toExample	= setTimeout('function()',iDelay);
    
    doSomething();
    This especially in cases where timeouts doesn't always exist. Normal logical ( if (toExample) { ... }) check causes errors.
    Zvona
    First Aid for
    Web Design

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    It was clearely a begginer's problem, so that I was not to show him a rafinated way... Sure... The existence of the setTimeout() can be also checked with a

    if(setT){

    }

    My intention was only to make him understand the process...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    New Coder
    Join Date
    Sep 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you both Kor and Zvona! (I know you just had my best interests at heart Kor

    Unfortunately, I have discovered this will not solve my problem, but it is good to know this for the future. Just again, thank you both.

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    feel free to let us know which is your detailed problem, maybe we can help you somehow, according to our knowledge.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    New Coder
    Join Date
    Sep 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post

    Well, by all means, you can take a stab at it.

    I currently have a function that writes several lines of text in a textbox by changing its value every time a character is added, anyway I am sure you are familiar with the script. (See below.) What I want to happen is when a line is completed, I want it to stop the function and call another that will recall the original function after a delay. (So I want it to pause for a while before continuing to display the next line.)

    Is there a better way of doing this? I am almost possitive there is. My entire code is below. Thanks for taking a look.

    Code:
    <HEAD>
     <TITLE>Script</TITLE>
     
      <SCRIPT LANGUAGE="JavaScript">
     <!--
    
    line = new Array("Line One", "Line Two", "Line Three", "Line Four")
    var num = 0
    var text = line[num]
    var temp
    var cha = 1
    var maxNumberofLines = 3
    function appear() {
    	
    	temp = text
    	if (cha <= text.length) {
    	text = text.substring(0,cha)
    	document.psword.txtarea.value = text
    	text = temp
    	cha = cha + 1
    	setTimeout('appear()', 80)
    	} else {
    	cha = 1
    	
    	}
    	
    }
    	
    function writing() {
    	num = num + 1
    	text += "\n \n" + line[num]
    	if(num >= maxNumberofLines) {
    	}else{
    	setTimeout('writing()', 500)
    	}
    }
     //-->
      </SCRIPT>
     </HEAD>
     
     <BODY>
      <center><FORM NAME="psword">
        <textarea name="txtarea" cols="70" rows="30"></textarea>
     </FORM>
    </center>
     <SCRIPT LANGUAGE="JavaScript">
     <!--
    	setTimeout('writing()', 200)
    	appear()
    //-->
      </SCRIPT>
     
    
     </BODY>

  • #8
    New Coder
    Join Date
    Sep 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Bump.

  • #9
    New Coder
    Join Date
    Sep 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't think anyone else is going to reply to this thread any more, but I thought I would give it one last try. Thanks again for all the help I have already recieved, you guys are great.

  • #10
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    function writing() {
    num = num + 1
    text += "\n \n" + line[num]
    if(num >= maxNumberofLines) {
    clearTimeout('setT')
    }else{
    var setT=setTimeout('writing()', 500)
    }
    }

    But it looks like you have no event which will call again your function(s). First time, as you insert the function in body, they are called as it will have been an onload. What about next time? Have you considered a key's event? (onkeyup, for instance?). Or you want the function to work undefinitely underground?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •