...

View Full Version : function argument auto increment



satish_j
10-14-2008, 12:58 PM
I came across a script on net that utilises the foll. syntax for calling setTimeoutfunction:
setTimeout("ScrollIt("+pos+")",pause);
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)!='#') {
txt+=message.charAt(pos);
pause=scrollspeed;
window.status=txt; }
else {
txt="";
pause=lineDelay;
if (pos==message.length-1) pos=-1;
}
pos++;
//status=pos;
setTimeout("ScrollIt("+pos+")",pause);
}

can anyone pls explain me this?

mcjwb
10-15-2008, 12: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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum