www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] How do I pass by value instead of reference?

  1. #1
    Join Date
    Dec 2009
    Posts
    3

    resolved [RESOLVED] How do I pass by value instead of reference?

    This code:

    Code:
    var ret = new Array();
    for (var i = 0; i < 3; i++) {
      window.setTimeout(
        function(){ ret.push(i); },
        1000
      );
    }
    window.setTimeout(function() {
    document.write("ret[0]="+ret[0]+" (expected 0)<br/>\n");
    document.write("ret[1]="+ret[1]+" (expected 1)<br/>\n");
    document.write("ret[2]="+ret[2]+" (expected 2)<br/>\n");
    },1500);
    Gives this output:

    Code:
    ret[0]=2 (expected 0)
    ret[1]=2 (expected 1)
    ret[2]=2 (expected 2)
    So it seems that i is passed by reference instead of value. How do I pass i by value?

  2. #2
    Join Date
    Feb 2006
    Posts
    2,926
    Its not i, its the way you are setting the timer.
    The timer itself should call its next iteration.

    The code is a lot ugler with setTimeout than setInterval,
    but you can substitute it if you want, and avoid having to clear it.

    Code:
    var ret= [];
    var timer= setInterval(function(){
        ret.push(ret.length+1);
        if(ret.length== 3){
            clearInterval(timer);
            alert(ret.join('\n'))
        }
        else ret.push(ret.length+1)
    },1000);
    Last edited by mrhoo; 12-15-2009 at 09:39 PM.

  3. #3
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by josteinaj View Post
    So it seems that i is passed by reference instead of value. How do I pass i by value?
    actually, numbers ARE passed by value, but you're not passing anything...

    when it fires, the timedout function looks to the only i it knows: the loop counter. by this time, the loop counter is set at the last position, so they are all 2.

    to remedy this unpleasant situation, i moved to Array.map and don't use loops anymore.


    but you can also take advantage of the fact that numbers are passed by value, and pass the value of i to a fresh i that your timeout function will see instead of the loop counter, simply by using another function to create private scope inside the loop:

    Code:
    var ret = new Array();
    for (var i = 0; i < 3; i++) {
     (function(i){
      window.setTimeout(
        function(){ ret.push(i); },
        1000
      );
     }(i));
    }
    window.setTimeout(function() {
    document.write("ret[0]="+ret[0]+" (expected 0)<br/>\n");
    document.write("ret[1]="+ret[1]+" (expected 1)<br/>\n");
    document.write("ret[2]="+ret[2]+" (expected 2)<br/>\n");
    },1500);
    this works as you had expected...
    Last edited by rnd me; 12-16-2009 at 02:54 AM.

  4. #4
    Join Date
    Dec 2009
    Posts
    3
    mrhoo: thanks, I actually didn't know about setInterval until now , thanks for the tip. I'll start using it another part of my code. However in my case the code inside the for-loop needs to be run in sequence for the three i's. The code contains a callback-section which needs to complete before the next iteration is started so I need to use setTimeout instead of setInterval to better control the timing and "interaction between the threads (in a way)".

    Until now I've been using a global variable to count each time the function is called but passing the i as a value would be more elegant.

    rnd_me: that's what I'm looking for, thanks!

  5. #5
    Join Date
    Dec 2009
    Posts
    3
    mrhoo: after a second look I see that your code of course also runs in sequence and would usually work in my case too. However if the callback-section in my code (not included in the example code) is not called and completed within 1000ms, then the next iteration would start and my script would fail. Would work for the example though.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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