Javascript - passing values to function

eggrock

Supreme [H]ardness
Joined
Dec 1, 2003
Messages
4,102
A couple of problems with this code:

Code:
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 values for count (0) and delay (5000) are passed with 'onload' in the body tag.

First problem, array index 1 is the first thing to display. Is 'i' incremented at the start of the for loop?

Next (biggest) problem, 'count' is undefined when the function calls itself via the setTimeout bit (so is delay).

What should happen is that everything in the 'newsitem' array gets listed starting with element 0, then looping through the array and displaying things line by line. Output goes to a text input box.

I'm a javascript n00b; the only things I've seen passed to functions via setTimeout are strings (or nothing) on any example I've been able to find.
 
Call me crazy, but wouldn't this result in new variables named count and delay being implicitly declared and passed to the function? You'd have to move count and delay out to being block-level variables, or else build the string that you're going to pass to setTimeout like so:

Code:
var timeoutCommand = 'newsbar(' + count + ',' + delay + ')';
setTimeout(timeoutCommand, delay);
 
Okay, I'm way off base on how I expect variables to be treated in this language. Nothing works how I would consider normal, eg C/Perl/reverse Polish notation on HP calculators... ;)

W3 schools doesn't give any examples other than strings... Supposedly '++' increments a variable, so if count is 0 and I say 'count++', it becomes 1. I can see that when I output the value. But if I then try to output 'newsitem[count]' it doesn't work.

So I think my big lack here is understanding *exactly* how variables are treated in Javascript, along with this funky build-a-string stuff to call a routine. Is there a good online source of information? I've been using W3 schools and various code snippets to attempt this.
 
Where/how do you initially define count and delay?

First thing you need to know is that the statements in a setTimeout are executed in the global scope. setTimeout acts as if the statements were wrapped in a <script> tag and appended to the end of the document at the appropriate time.

So, if count and delay are local to newsbar, the when your timed-out call is executed, it will error out with count undefined (undefined in the global scope).

If you make them global variables, then newsbar's identically named arguments will mask access to the globals within the function's scope. Meaning that the local count would be incremented, not the global. But, since the statements in a setTimeout are execute in the global scope, newsbar's local count would be set to the value of the (unincremented) global count each time newsbar is called.

If you start with something like
Code:
var count=0;
var delay=500;
function newsbar()
and make count and delay global, you avoid a lot of struggle.

If you define your array of string globally
Code:
var newsitem = new Array(); 
newsitem[0] = "some news";
newsitem[1] = "more news";
var count=0;
var delay=500;
function newsbar()
you'll save some CPU time. There's no need to allocated memory and initialize the the array/strings every time the function is called.

And, of course, the loop is unnecessary.
Code:
var newsitem = new Array(); 
newsitem[0] = "some news";
newsitem[1] = "more news";
var count=0;
var delay=500;
function newsbar()
{
  document.newsform.newsinput.value=newsitem[count];
  count++;
  if(count>=newsitem.length)
  {
    count = 0;
  }
  news=setTimeout("newsbar()", delay);
}

JavaScript Kit has an assortment to decent beginner tutorials that cover most of the task you'll want to do.
 
Excellent, that's what I needed to know.

I already moved count and delay and the array declaration/initialization to the global scope. Never even thought about removing the loop but that's definitely unnecessary. Time to get this thing working.

So.. Some things run in the global scope.. I'm going to want a book on this eventually. I don't plan on using Javascript if I can help it but it's always good to learn. [edit] And learn what works in IE vs. Firefox vs. Opera yada yada.. Same thing I'm running into with CSS but the documentation is easier to find.

Thank you!

[edit] Yep, that did the job. Much appreciated.
 
Back
Top