Click to See Complete Forum and Search --> : For loop


uruba_uk
11-30-2004, 09:06 AM
Is there any equivilant in javascript to the DoEvents statement in VB?

I have a loop that takes a long time to run and I want to show the user that the loop is running, something like a progress bar or a counter.

I have tried the following code but the text element is not updated till the loop has finished.

for(i=0;i<1000;i++) {
text1.value=i.toString()

//rest of my code is here

}

Any ideas would be be much appreciated.

JPnyc
11-30-2004, 09:17 AM
Try this:
var i = 0;
if(i < 1000) {
text1.value=i.toString();
i++;
}

uruba_uk
11-30-2004, 09:18 AM
Thanks for the reply.

Should this code go inside my loop then?

JPnyc
11-30-2004, 09:19 AM
That's in place of the loop you have there. I don't know what else you have. You have to post the code for me to tell.

uruba_uk
11-30-2004, 09:37 AM
Here is the code. It is a recursive function that is looping round nodes in a tree. It is building up a string a values for each node that is then put in a hidden field and submitted to the web server for processing.

function saveSettingsRecursive(folderObj) {

var childObj
var sNode = ''
var i

if(folderObj.checked) {
if(folderObj.tag!='') {
sNode = folderObj.linkKey + ',' + folderObj.tag + '|'
}
}

for (i=0 ; i < folderObj.nChildren; i++) {
childObj = folderObj.children[i]
if (typeof childObj.setState != "undefined") {
saveSettingsRecursive(childObj)
}
}
}

What I want to do is have some kind of progress indicator on the form that shows the user the progress of the loop. This routine however seems to lock the whole browser e.g. animated gifs on the page stop animaiting

JPnyc
11-30-2004, 09:46 AM
Just change the loop to be like the one I posted. Instead of a normal for loop, just use the primitive form I gave, with the incrementing coming LAST, after you show the value of i. It should force the printing of i in the txt field.

JPnyc
11-30-2004, 09:48 AM
Oh, and btw, animated Gifs WILL cease. A browser is not a multi-threaded app. when it's busy running a script, it has to cease other things.

uruba_uk
11-30-2004, 09:53 AM
I am sorry but will have to forgive my lack of knowledge, I am kind of new to this. I do not see how :

var i = 0;
if(i < 1000) {
text1.value=i.toString();
i++;
}

can replace my loop.

JPnyc
11-30-2004, 10:03 AM
Well look at what I did. I took the compact for loop, and broke it up. The initialization is now at top with the declarations, var i = 0;
then i put the test in an if statement, if(i < 1000), then the incrementing went dead last. In a normal for loop, those are all together in one line, see? Just identify the pieces I mention in your loop, and break them up as I did.

ccoder
11-30-2004, 10:11 AM
Originally posted by uruba_uk
I am sorry but will have to forgive my lack of knowledge, I am kind of new to this. I do not see how :

var i = 0;
if(i < 1000) {
text1.value=i.toString();
i++;
}

can replace my loop.
It can't!

The if statement will only execute 1 time. I think he meant to suggest a while loop: while (i < 1000)

uruba_uk
11-30-2004, 10:12 AM
Thats what I thought. I added a while loop with the suggested code inside, but the text element is still updated at the end on the loop rather than while the loop is executing.

JPnyc
11-30-2004, 10:20 AM
Incorrect, gentlemen. That loop will execute until i = 1000. Trust me, or if you don't, try it yourself.

JPnyc
11-30-2004, 10:21 AM
Man, this board can be a pain. Can't even delete your OWN posts!

uruba_uk
11-30-2004, 10:24 AM
I have tried the code and it does not loop. I even replaced the text.value = i.toString() to an alert to see what was happening. I get and alet telling me i = 0 then thats it.

JPnyc
11-30-2004, 10:27 AM
It has to. Please post exactly what you implemented.

uruba_uk
11-30-2004, 10:28 AM
var i = 0;
if(i < 1000) {
alert(i.toString());
i++;
}

JPnyc
11-30-2004, 10:33 AM
Sorry, I thought you'd put this in a function. I was just trying to demonstrate approach, not give you ready made code. I don't generally do that.

Try:
var i = 0;
function showi() {
if(i < 1000){
alert(i);
i++;
}}

Of course you know that you have to actually call the function from somewhere.

uruba_uk
11-30-2004, 11:00 AM
I am still not quite sure how it will work (lack of knowledge on my part).

I have found a temporary workaround for the moment. Outputting the value of i to the status bar (window.status = i.toString()).

Thank you for taking time out to help me. If I get some time I will re-look at your solution and try to figure it out.

JPnyc
11-30-2004, 11:24 AM
Ok. You don't actually need toString() though. It's redundant in javascript. Most scripting languages do those conversions automatically. VBscript does also, I believe.