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
Post a Comment