javascript array timed -


i'm missing little thing.. prints array doesn't wait in between lines.

<script type="text/javascript">  function showlines() { arr = [   "hi!",   "welcome!",   "hello!" ]  var duration=2000;  document.getelementbyid("quotes").innerhtml=arr; settimeout('showlines()',duration);  } </script> 

most answers here re initializing array on each iteration.it makes no sense that. should this:

<script type="text/javascript">  function showlines(){      var arr =     [       "hi!",       "welcome!",       "hello!"     ], = 0;      (function showlineshelper(){         document.getelementbyid("quotes").innerhtml += arr[i++]+'<br />';         if(i < arr.length)             settimeout(showlineshelper, 2000);     })();  }  </script> 

this way works, , array, , initialized once.

edit in response comment:

<script type="text/javascript">  function showlines(){      var arr =     [["hi!", 3000],      ["welcome!", 500],      ["hello!", 1000]]     , = 0;      function showlineshelper(){         document.getelementbyid("quotes").innerhtml += arr[i++][0]+'<br />';         if(i < arr.length)             settimeout(showlineshelper, arr[i][1]);     }      settimeout(showlineshelper, arr[0][1]); }  </script> 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -