Jump to content

setTimeout help!


rawb

Recommended Posts

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...

[code]
<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>
[/code]

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

[code]
//change the image!
tick(imagearray[i], textarray[i], 'change');
[/code]
Link to comment
Share on other sites

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
[code]
setTimeout("changediv(imagearray, textarray, "+i+")", 2000);
[/code]
Link to comment
Share on other sites

[!--quoteo(post=387318:date=Jun 23 2006, 06:56 PM:name=nogray)--][div class=\'quotetop\']QUOTE(nogray @ Jun 23 2006, 06:56 PM) [snapback]387318[/snapback][/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
[code]
setTimeout("changediv(imagearray, textarray, "+i+")", 2000);
[/code]
[/quote]


Works like a charm. Thanks a million man, I really appreciate it.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.