Timed interval loops in Javascript ?

  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

So some of you might already be aware that I am writing a XML parser in JavaScript. Things are going ok, but I need to parse each item at like a 5 second interval. Here is the for loop i am using:

JAVASCRIPT Code: [ Select ]
for (var i = 0; i < xmlrows.length; i++) {
 
var xmlrow = xmlrows[i];
var XmlState = xmlrow.getElementsByTagName("state")[0];
var TextState = XmlState.firstChild.data;
document.write(TextState+'<br/>');
 
}
 
  1. for (var i = 0; i < xmlrows.length; i++) {
  2.  
  3. var xmlrow = xmlrows[i];
  4. var XmlState = xmlrow.getElementsByTagName("state")[0];
  5. var TextState = XmlState.firstChild.data;
  6. document.write(TextState+'<br/>');
  7.  
  8. }
  9.  


I have tried using setTimeout() like so, but this only prints once.

JAVASCRIPT Code: [ Select ]
 
var c=0;
var t;
function timedCount()
{
c=c+1;
t=setTimeout("timedCount()",5000);
 
var xmlrow = xmlrows[i];
var XmlState = xmlrow.getElementsByTagName("state")[0];
var TextState = XmlState.firstChild.data;
document.write(TextState+'<br/>');
 
}
timedCount();
 
  1.  
  2. var c=0;
  3. var t;
  4. function timedCount()
  5. {
  6. c=c+1;
  7. t=setTimeout("timedCount()",5000);
  8.  
  9. var xmlrow = xmlrows[i];
  10. var XmlState = xmlrow.getElementsByTagName("state")[0];
  11. var TextState = XmlState.firstChild.data;
  12. document.write(TextState+'<br/>');
  13.  
  14. }
  15. timedCount();
  16.  


I tried using setInterval() but that only works with functions and functions don't loop through arrays. So my question is, how do I loop through an array at a set interval? Like, loop through a new array item every 3 seconds?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

You need a function that removes the node the current iteration will work with from the XML and then works with it.

You need a variable to keep a reference to an interval created using setInterval.

You function needs to be aware of and clear that interval if it finds nothing left to work with.

It might look something like this, of course substituting the named function calls with your routines for obtaining that data.

JAVASCRIPT Code: [ Select ]
var myXML = xml_to_work_with();
var processor = function()
{
   if(myXML.childNodes.length < 1)
   {
      clearInterval(processing);
      return;
   }
   var node = myXML.removeChild(myXml.firstChild)
   work_with(node);
}
var processing = setInterval(processor, 5000);
  1. var myXML = xml_to_work_with();
  2. var processor = function()
  3. {
  4.    if(myXML.childNodes.length < 1)
  5.    {
  6.       clearInterval(processing);
  7.       return;
  8.    }
  9.    var node = myXML.removeChild(myXml.firstChild)
  10.    work_with(node);
  11. }
  12. var processing = setInterval(processor, 5000);
  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

JoeBert - Thanks for your reply, however, i am completely lost now! LOL! Maybe it might help if you could show me where in your solution the code prints with
Code: [ Select ]
document.write(TextState+'<br/>');
Then I might have a better idea following what you are saying and working it in to my implementation! But thank you so much for your help!
  • mk27
  • Proficient
  • Proficient
  • User avatar
  • Posts: 334

Post 3+ Months Ago

RedBMedia wrote:
I tried using setInterval() but that only works with functions and functions don't loop through arrays.


Yeah, the js timers are kind of neat because they fork and run in the background, but that makes them awkward because there is no actual sleep() type function.
JAVASCRIPT Code: [ Select ]
var xmlrows = new Array; /* global */
function doRow (i) {
    ...all your stuff with xmlrows[i]...
    if (i < xmlrows.length-1) {
         i++;
         setTimeout("doRow("+i+")",5000);
    }
}
...populate xmlrows...
doRow(0);
 
  1. var xmlrows = new Array; /* global */
  2. function doRow (i) {
  3.     ...all your stuff with xmlrows[i]...
  4.     if (i < xmlrows.length-1) {
  5.          i++;
  6.          setTimeout("doRow("+i+")",5000);
  7.     }
  8. }
  9. ...populate xmlrows...
  10. doRow(0);
  11.  

Does that make sense? Another limitation of the timer functions is that you can only pass arguments that are string literals or numbers thru them; you can't pass an object or variable directly. So the array, "xmlrows" has to be a global (unless someone knows of some work around for that).
  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

mk27 - thanks for your help. I am going to try and implement this in a moment.

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 123 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.