SitePoint Sponsor |
|
User Tag List
Results 1 to 11 of 11
-
Mar 13, 2007, 22:07 #1
- 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
-
Mar 13, 2007, 22:53 #2
- 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.
-
Mar 13, 2007, 23:08 #3
- 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
-
Mar 14, 2007, 02:55 #4
- 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.
-
Mar 14, 2007, 07:24 #5
- 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);
-
Mar 14, 2007, 12:14 #6
- Join Date
- Mar 2005
- Location
- USA
- Posts
- 5,482
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by 7stud
You can also pass a function (recommended) instead of a String as the first argument
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.
-
Mar 14, 2007, 12:18 #7
- 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.
-
Mar 14, 2007, 12:35 #8
- 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);
-
Mar 14, 2007, 13:10 #9
- 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?
Originally Posted by Pepejeria
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.
-
Mar 14, 2007, 13:34 #10
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Mar 14, 2007, 17:30 #11
- Join Date
- Mar 2001
- Posts
- 3,537
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You're right of course. I left out the "internal" quote marks that make it work:
Code:"count('" + someVar + "')"
Code:var someVar = 10;
Code:count(' + 10 + ')
Code:count('10')
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;
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 03:25.
Bookmarks