Using the following code, I expect to have varying bits of information placed on the page every few seconds:
>function newsbar(count, delay)
{
var newsitem = new Array();
newsitem[0] = "some news";
newsitem[1] = "more news";
for (i=0;i<newsitem.length;i++)
{
if(count==i)
{
document.newsform.newsinput.value=newsitem[i];
count++;
if(count>=newsitem.length)
{
count = 0;
}
news=setTimeout("newsbar(count, delay)", delay);
}
}
}
The script is called in the body tag, eg <body onload="newsbar(0, 5000);">, and placement is in a text input box (form name is 'newsform' and the input name is 'newsinput'.
A couple problems:
- Even though I pass '0' (as count) in the onload tag, array element 1 displays. This tells me that 'i' is being incremented differently than I'd expect; guessing as soon as 'i++' is encountered. Not a huge deal.
- The big problem is, when setTimeout is used to call newsbar again, count (and probably delay) are undefined. I don't know what's going on there. Every example I see on the web is either passing a string to the function (which works in my case) or nothing at all. No actual variable values are passed so this has to be yet another feature *coughidiocycough* of Javascript, or maybe just a quirk of setTimeout or whatever, I dunno.
For efficiency's sake I can probably initialize that array externally to the function but since I'm basically illiterate when it comes to Javascript I'm playing it safe for now. The rules in Javascript are so incomprehensible I'm willing to sacrifice elegance and efficiency in the name of functionality.