www.webdeveloper.com
Results 1 to 6 of 6

Thread: Accessing closures in setTimeout

  1. #1
    Join Date
    Jul 2006
    Posts
    60

    Accessing closures in setTimeout

    Hi - I wonder if anyone can help me with something that's been bugging me for a long time. When writing JS, I try to encapsulate my code somehow. For instance, in the example below, I have a function ('testFunc') that's encapsulated within jQuery's document.ready callback function :

    Code:
    $(function(){
    		var testFunc = function(x){
    			x++;
    			return x;	
    		}
    });
    If I then start using JS's base timer functions like settimeout, I've always been unsure of how to access the 'testFunc' function. As I understand it, even if the timer functions are called inside the same function, as in...

    Code:
    $(function(){
    		var testFunc = function(x){
    			x++;
    			return x;	
    		}
    		
    		var timer = setTimeout("testFunc", 100);
    });
    ... they will lose the scope of the jQuery callback - instead the timer functions will only be able to access objects in the global scope.

    Does anyone have any ideas/advice on best practise for how to solve this kind of problem?

    Thanks for any help!

    Nick

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    But you have sent no argument to that function. Maybe you wanted something like
    Code:
    var arg=1;
    var that=this;
    var timer = setTimeout(function(){that.testFunc(arg)), 100);
    or something like that

  3. #3
    Join Date
    Jul 2006
    Posts
    60
    Doh! Thanks for your reply. Sorry, I did indeed forget to send an argument...

    Will experiment with your suggestion..!

  4. #4
    Join Date
    Jul 2006
    Posts
    60
    Hi - am afraid I couldn't get that last suggestion to work. If anyone's interested however, I 'think' I've got something reasonably simple to work, using the Singleton pattern. I don't know if it's a bit over the top, but it seems to work...

    Code:
    var NSD = (function() {
    	return {
    		testFunc: function(x){
    			x++;
    			document.write (x);
    		},
    		onload: $(function(){
    		var a = setTimeout('NSD.testFunc(1)', 1000);			
    		})
    	}
    })();
    'NSD' here is just a namespace used to encapsulate all my functions. Because 'testFunc' is now a public function (note, not a global one), the 'setTimeout' function can access it.

    You can probably tell I'm just experimenting - I'm sure there are more elegant ways of doing this, so any suggestions are welcome!!

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    My suggestion was otherwise. Try:
    Code:
    var a = setTimeout(function(){NSD.testFunc(1)}, 1000);

  6. #6
    Join Date
    Jul 2006
    Posts
    60
    Thanks - yes, I should write it using the anonymous function as you suggest

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles