...

View Full Version : stopping a setTimeout()?



greenham
10-18-2005, 01:53 PM
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. :)

Kor
10-18-2005, 02:56 PM
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

Zvona
10-18-2005, 03:02 PM
I think more proper way of clearing timeout should be done like this:


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.

Kor
10-18-2005, 03:05 PM
:D 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... :thumbsup:

greenham
10-19-2005, 12:10 PM
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. :)

Kor
10-19-2005, 12:12 PM
feel free to let us know which is your detailed problem, maybe we can help you somehow, according to our knowledge.

greenham
10-20-2005, 10:40 AM
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.


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

greenham
10-22-2005, 01:42 AM
Bump.

greenham
10-23-2005, 03:41 AM
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. :)

Kor
10-24-2005, 08:44 AM
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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum