Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
syphon^2
Sep 22, 2004
I hope Javascript questions are welcome here. There's no "Javascript Megathread", and this question is very very simple, so I didn't want to start a new thread just for it.

I'm very new to Javascript (Perl's my language of choice), but I'm writing a web-app using Perl's CGI::Ajax, which enables Ajax by mapping Javascript functions on your web page to Perl functions.

Anyhoo, the Javascript functions need to have their parameters sorted in a particular way for CGI::Ajax. I'm having problems with this... so I'm hoping you guys can help me.
code:
var scalar = 'foo';
var array = ['yabba','dabba','doo'];

//does not work!
functionToInvoke( [scalar,array], ['ouput_div'] );

//kinda works, but only feeds in the first element of the array
functionToInvoke( [scalar,array[0]], ['ouput_div'] );

//yay!
functionToInvoke( [scalar,'yabba','dabba','doo'], ['ouput_div'] );
As you can see, functionToInvoke requires 2 arguments to work. A list of parameters to feed into the Perl function, and a div (or list of divs) to put the output into.

In my example, I need to feed the following list of parameters into the Perl function - ['foo','yabba','dabba','doo']. Hard coding all the values works fine, or even hard coding a specific array element works. However, just providing the array (with the hope that it'd just iterate through each value) doesn't work. It's hard to tell exactly what it's feeding into functionToInvoke(), but it looks like it's trying to feed the index and then element.

Any ideas?

EDIT: Just a bit more information... I tried
code:
alert(scalar,array);
and it output "foo,yabba,dabba,doo" (pretty much exactly what I want). I'm not sure why the alert function is parsing it this way, but the CGI::Ajax function is not.

syphon^2 fucked around with this message at 15:30 on Apr 22, 2008

Adbot
ADBOT LOVES YOU

syphon^2
Sep 22, 2004

Fehler posted:

You might be looking for the concat() method. Try something like this:

code:
var a = new Array(scalar); //create new array for scalar
a = a.concat(array); //append array

functionToInvoke(a, ['ouput_div'] );
That worked! Thanks!

How come alert seemed to display my data properly, but when supplying it in the same manner for functionToInvoke, it failed? Maybe something to do with how CGI::Ajax works?

syphon^2
Sep 22, 2004
EDIT: ^^^^^ Duly noted (right on the heels of another post). Thanks for the tip. :)

Another javascript question...

I'm trying to use setInterval() and setTimeout() to create a basic timer. I want my page to run doIt() every 10 seconds for 30 minutes, and then stop. (although I have the times cut down drastically for testing purposes)
code:
function autoDoIt() {
	//do it every 2 seconds
	var intervalID = setInterval( "doIt();", 2000 ); 
	//set a timeout, so that after 10 seconds, the 'intervalID' loop is terminated.
	setTimeout('clearInterval(intervalID)',10000);
}
The interval gets set properly... and doIt() is run every 2 seconds as it should. However, after 10 seconds, I simply get an error "'intervalID' is not defined", and doIt() keeps getting run... forever.

Note that if I change
code:
setTimeout('clearInterval(intervalID)',10000);
to
code:
clearInterval(intervalID);
Everything works great (although my interval is terminated before it ever gets a chance to do anything).

This seems like a pretty simple use-case. What am I doing wrong?

syphon^2
Sep 22, 2004

Fehler posted:

Try making intervalID global, i.e. add "var intervalID;" at the top outside the function.

Awesome... that seems to have done the trick!

One last time... before I stop pestering you java gurus with js questions... why did this happen? I mean, obviously having intervalID as an argument within an argument brought it out of scope from where it was declared... but why would it do that?

Also, how can I reference the intervalID scoped inside the autoDoIt() function? Given the layout of my .js file... I really really want to avoid having a single random global floating around there. :)

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply