Jump to content


Photo

setTimeout help!


  • Please log in to reply
2 replies to this topic

#1 rawb

rawb
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts

Posted 23 June 2006 - 07:50 PM

Hello! I have a script written to change the information in a div every few seconds (via innerhtml), but for some reason it keeps giving me a "Stack overflow at line: 33" error. I really don't know what this means and could use some help.. if anyone could tell me what is wrong here I would appreciate it a lot. It's probably something really dumb and simple but I just need a fresh pair of eyes to look it over. Thanks in advance!

Oh.. the onClick buttons work like a charm, so there's nothing wrong with the 'tick' function.. I think...

<script>

//create two parallel arrays
imagearray = new Array(3);
textarray = new Array(3);

imagearray[0] = "corporate.gif";
imagearray[1] = "hosting.gif";
imagearray[2] = "community.gif";

textarray[0] = "corp";
textarray[1] = "host";
textarray[2] = "commute?";



function tick(newimage, newtext, div) {
//takes three string arguments, div being a reference to the div to be changed

var replacement = "image: " + newimage + "<br>text: " + newtext + "<br>";

document.getElementById(div).innerHTML = replacement;

}



function changediv(imagearray, textarray, i) {
//takes two array arguments and one number (the number in the parallel arrays on which to begin)


//change the image!
tick(imagearray[i], textarray[i], 'change');

//calculate the next number in the array, or start over...
i++;
if (i >= imagearray.length) {
  i = 0;
}

// call this function again with a new number after so many seconds...
setTimeout(changediv(imagearray, textarray, i), 2000);

}

</SCRIPT>

<body onLoad="changediv(imagearray, textarray, 2);">

<a href=# onclick="tick(imagearray[1], textarray[1], 'change'); return false;">test</a>
<a href=# onclick="tick(imagearray[2], textarray[2], 'change'); return false;">test2</a>

<div id="change">


image: none<br>
text: none<br>


</div>

By the way, these are lines 32 and 33 (it says the 'stack overflow' error is on 33).

//change the image!
tick(imagearray[i], textarray[i], 'change');


#2 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 23 June 2006 - 11:56 PM

The error happens because you are calling the script in the setTimeout instead of using a string type.

All you need is to change your setTimeout like this
setTimeout("changediv(imagearray, textarray, "+i+")", 2000);

NoGray.com


#3 rawb

rawb
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts

Posted 24 June 2006 - 01:15 PM

[!--quoteo(post=387318:date=Jun 23 2006, 06:56 PM:name=nogray)--][div class=\'quotetop\']QUOTE(nogray @ Jun 23 2006, 06:56 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
The error happens because you are calling the script in the setTimeout instead of using a string type.

All you need is to change your setTimeout like this
setTimeout("changediv(imagearray, textarray, "+i+")", 2000);
[/quote]


Works like a charm. Thanks a million man, I really appreciate it.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users