Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2008
    Location
    India
    Posts
    88
    Thanks
    3
    Thanked 0 Times in 0 Posts

    function argument auto increment

    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:
    Code:
    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?
    Last edited by Kor; 10-14-2008 at 02:48 PM. Reason: wrap the code [code][/code]

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Code:
    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:
    Code:
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •