#1
  1. web.graphic.print
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    Lancaster, CA USA
    Posts
    118
    Rep Power
    115

    Problem with setInterval and this


    I've got an interval set within a function which uses a function (this.functionName). The function is not able to reach variables (this.variableName) from within itself when called from the setInterval. Any ideas?
    Fire Minded
    Brent Bevans
    661.974.6771
    43654 Colony dr., Lancaster, CA 93536
    fire.minded.design@gmail.com
    http://www.fireminded.com
  2. #2
  3. web.graphic.print
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    Lancaster, CA USA
    Posts
    118
    Rep Power
    115
    Only way I found to get around it was to set the intervals outside of the function.
    Fire Minded
    Brent Bevans
    661.974.6771
    43654 Colony dr., Lancaster, CA 93536
    fire.minded.design@gmail.com
    http://www.fireminded.com
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    26
    Rep Power
    0
    Notice that "this"
    equals the window ...
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
    <div id="dsply"></div>
    <script >
    (function (){
     var a = "Time = ";
     function repeat(){dsply.innerHTML = a + new Date + "<br>this = " + this} 
     setInterval(repeat,1000);
    }())
    </script>
    </body>
    </html>
  6. #4
  7. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,953
    Rep Power
    4033
    You have to bind your functions to the correct context in order to use this within them. Eg:
    Code:
    setInterval(this.someFunction.bind(this), 1000);
    Without the bind, setInterval will just execute the function in the global context (which makes this equal to the window object).
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  8. #5
  9. web.graphic.print
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    Lancaster, CA USA
    Posts
    118
    Rep Power
    115
    Ok got another solution.

    Code:
    setInterval/setTimeout(function(this), 1);
    edit: Wait that doesn't work lol don't know why i thought it did
    Last edited by fireminded; November 12th, 2013 at 11:47 AM.
    Fire Minded
    Brent Bevans
    661.974.6771
    43654 Colony dr., Lancaster, CA 93536
    fire.minded.design@gmail.com
    http://www.fireminded.com
  10. #6
  11. web.graphic.print
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    Lancaster, CA USA
    Posts
    118
    Rep Power
    115

    Smile


    Oh word

    Code:
    setInterval(this.function.bind(this), frame_rate);
    works
    Fire Minded
    Brent Bevans
    661.974.6771
    43654 Colony dr., Lancaster, CA 93536
    fire.minded.design@gmail.com
    http://www.fireminded.com

IMN logo majestic logo threadwatch logo seochat tools logo