Timed interval loops in Javascript ?

  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Joined: May 01, 2007
  • Posts: 315
  • Status: Offline

Post August 30th, 2009, 3:34 pm

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?
Joe Hall
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post August 30th, 2009, 3:34 pm

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post August 30th, 2009, 3:59 pm

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);
Strong with this one, the sudo is.
  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Joined: May 01, 2007
  • Posts: 315
  • Status: Offline

Post August 30th, 2009, 4:50 pm

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!
Joe Hall
  • mk27
  • Proficient
  • Proficient
  • User avatar
  • Joined: Jun 09, 2009
  • Posts: 334
  • Status: Offline

Post August 31st, 2009, 7:06 am

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
  • Joined: May 01, 2007
  • Posts: 315
  • Status: Offline

Post August 31st, 2009, 7:46 am

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

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: Kurthead+1 and 163 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
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.