View Full Version : function argument auto increment

10-14-2008, 01:58 PM
I came across a script on net that utilises the foll. syntax for calling setTimeoutfunction:
where,ScrollIt is the function that accepts pos as argument and pause has a value of 100.
My question is How does the above syntax work???
I have never come across this syntax in any ebook.As you can see,it calls the function and then assigns the argument value using plus operator.This asignment is not clear to me.The entire code of function is as follows:

var message="Welcome to my Blog.#"+"Pls enter your Login Credentials.#"+"#";
var scrollspeed=85;
var lineDelay=2000;
var txt="";
function ScrollIt(pos)
if (message.charAt(pos)!='#') {
window.status=txt; }
else {
if (pos==message.length-1) pos=-1;

can anyone pls explain me this?

10-15-2008, 01:36 AM
setTimeout("ScrollIt("+pos+")", pause);
The above code works because the setTimeout function accepts 2 arguments, the first being a string and the second being an integer.

The first argument is a simply a string that contains a javascript statement. This string is like any other string in javascript and can be made up in exactly the same way. So the above code could be written like so:

var JSstring = "ScrollIt("+pos+")";
setTimeout(JSstring, pause);

If you "alerted" the JSstring variable you would get (assuming pos=1) "ScrollIt(1)". It is this final string that will be executed by the setTimeout function, the variable "pos" is not passed to the setTimeout function as a paramater.

Hope that helps.