PDA

View Full Version : delay in script - how


scroots
12-30-2002, 07:23 PM
i have the following code
[code]
function ChangeMsg() {
msgn++;
document.f.k.value=""
if (msgCnt < msgn) {
msgn = 0;
}
}
[\code]
how can i add a delay before the textbox is cleared? i have tried numerous methods .

anyone have an ideas?

scroots

joeframbach
12-30-2002, 07:28 PM
setTimeout('document.f.k.value=""',time-in-milliseconds)

example:
setTimeout('document.f.k.value=""',1000) will clear the box in 1 second

scroots
12-30-2002, 07:33 PM
thanks,
it sounds a nice plan it would normally work except when its my dodgy coding project.

scroots

scroots
12-30-2002, 07:36 PM
you see what i would like to do is clear the textbox each time the function is run.
i would like the delay before the text box is cleared.

does this help any more?
scroots

beetle
12-30-2002, 07:53 PM
Originally posted by scroots
you see what i would like to do is clear the textbox each time the function is run.
i would like the delay before the text box is cleared.Isn't that what joe showed you?

I'm confused.

There is no good way to create a delay without setTimeout

scroots
12-30-2002, 08:01 PM
i will explain again hopefully a little clearer.
i have a script that writes to a text box from an array when it is to move to the next item it calls the function ChangeMsg() . I would like to wait before the text box is cleared so people may actually be able to read the content. if i set thr time out to say 4 seconds it would initiate and then 4 seconds later make the text box blank and then 4 seconds after that even if it was in the middle of writing a message it would wipe it blank and continuelly wipe it blank every 4 seconds.

so in the function ChangeMsg() i would like to have a pause which only works when the function (ChangeMsg()) is called. the pause function needs to stop for the time limit and then continue on processing the script.

scroots

joeframbach
12-30-2002, 09:08 PM
var myArray=["item1","item2","item3","item4"];
for(var i=0;i<myArray.length;i++)
setTimeout('document.getElementById("foo").value=myArray[i]',4000);

<input type=text id=foo>


is this what you want?

scroots
12-30-2002, 09:12 PM
i have fixed the problem now by making the timeout the length of the text to be put in.

thank you for your help.

scroots

joeframbach
12-30-2002, 09:13 PM
im really sorry - that doesnt wwork. here is one that does.
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
var myArray=["item1","item2","item3","item4"];
function go()
{
for(var i=0;i<myArray.length;i++)
setTimeout('document.getElementById("foo").value=myArray['+i+']',(i+1)*1000);
}
-->
</SCRIPT>
</HEAD>
<BODY onLoad=go()>
<input type=text id=foo>
</BODY>
</HTML>

Graeme Hackston
12-30-2002, 11:55 PM
This is timely. I've been playing with JavaScript, ActiveX and VBScript today. This is not the way you want to go but I found that a JS function waits for a VB function to complete.

I don't know much about VB but I think it can wait for a condition to change. The 2nd alert will actually fire after the VB function is complete.

function JS_function() {
alert('1')
VB_function() // an operation that takes a couple seconds
alert('2')
}

joeframbach
12-31-2002, 03:26 AM
what if you have ALOT of stuff to show the user? oh - the for loop, nvm i forgot