SitePoint Sponsor

User Tag List

Results 1 to 11 of 11

Hybrid View

  1. #1
    SitePoint Addict chartahir's Avatar
    Join Date
    Oct 2006
    Location
    Karachi, Pakistan
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    setTimeout("count()",1000) function call.

    setTimeout("count()",1000);

    setTimeout function can be used to call a function without any parameters easily, but is there ant way i can pass a parameter to function count inside function setTimeout ???
    chartahir

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like this?
    Code:
    setTimeout("count('a string parameter',99)",1000);
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Addict chartahir's Avatar
    Join Date
    Oct 2006
    Location
    Karachi, Pakistan
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes thats like what i want but,when passing parameters to count through setTimeout the parameters are received as constant.

    Can i pass variables ?
    chartahir

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "count(" + someVar + ")"

    It's gonna be a string on the other side of the worm hole.

  5. #5
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also pass a function (recommended) instead of a String as the first argument
    Code:
    window.setTimeout(function() {
        count(someVar);
    }, 1000);

  6. #6
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud
    "count(" + someVar + ")"

    It's gonna be a string on the other side of the worm hole.
    Really? If I pass a string like that I get errors. That would work for numbers, but they aren't passed as strings.

    You can also pass a function (recommended) instead of a String as the first argument
    That doesn't work in IE5/Mac or Safari 1.0 though.

    Browsers other than IE and Safari 1.0 support more than 2 arguments to setTimeout. After the first two, the others will be passed as arguments to the function specified in the first argument, if you just pass a reference to a function (not a string or a function call) as the first argument.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kravvitz View Post
    Really? If I pass a string like that I get errors. That would work for numbers, but they aren't passed as strings.
    You're right of course. I left out the "internal" quote marks that make it work:
    Code:
    "count('" + someVar + "')"
    The way that works is if you have:
    Code:
    var someVar = 10;
    then the value for someVar is substituted in the code, giving you:
    Code:
    count('   +  10  +  ')
    then the concatenation happens, forming the string:
    Code:
    count('10')
    Forcing js to evaluate the string immediately serves to freeze dry the current value of someVar into the setTimeout().
    Code:
    function show(str)
    {
    	alert(str);
    }
    
    var data1 = "a string";
    var data2 = 10;
    setTimeout("show('" + data1 + "')", 1000);
    setTimeout("show('" + data2 + "')", 2000);
    
    //The following execute before the setTimeout():
    data1 = "a different value";
    data2 = 156;
    Compare to:
    Code:
    function show(str)
    {
    	alert(str);
    }
    
    var data1 = "a string";
    var data2 = 10;
    setTimeout("show(data1)", 1000);
    setTimeout("show(data2)", 2000);
    data1 = "a different value";
    data2 = 156;
    Last edited by 7stud; Mar 15, 2007 at 02:25.

  8. #8
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, Safari 1 fails that, crap. IE 5/Mac is pretty dead though. Thank god. RIP.

  9. #9
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If safari1 support is a must, you could push the function to a stack, with something like this:
    Code:
    var Callback = {
      stack: [],
      setTimeout: function(fn, timeout) {
        var id = stack.length;
        Callback.stack[id] = fn;
        return window.setTimeout("Callback.__call("+id+")", timeout);
      },
      __call: function(id) {
        var fn = Callback.stack[id];
        Callback.stack[id] = null;
        fn();
      }
    };
    Callback.setTimeout(function() {
        count(someVar);
    }, 1000);
    I wouldn't care too much about archaic browsers though.

  10. #10
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's a very interesting alternative, kyberfabrikken. I hadn't thought of doing that. Did you come up with that yourself?

    Quote Originally Posted by Pepejeria
    Ah, Safari 1 fails that, crap.
    Safari 1.2+ supports it though. (I don't know about 1.1.)
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  11. #11
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kravvitz View Post
    Did you come up with that yourself?
    I think I did.


Bookmarks

Posting Permissions

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