Home Ask Login Register

Developers Planet

Your answer is one click away!

Elliott McNary February 2016

Assigning Object in function to Variable or call setState w/o this?

I'm sure this is rather simple, but with me being new to JS and React I can't seem to find an answer to this.

Basically, I have a function that uses the JQ animate method to act as a counter of sorts. The function looks like this:

$({countNum: 99}).animate({countNum: 1000}, {
     duration: 2000,
     step: function() {
      var counter = Math.floor(this.countNum)
     complete: function() {
       return 'counter done';

I am trying to setState within the step so that I can simulate a counter on my app as it counts up to a specific number. In order to set the state I need to bind this to the object in the animate function, however then I can't figure out how to access the countNum so that it increases.

Any ideas? Or am I approaching this totally wrong.


Marc February 2016

You can assign "this" to a specific variable before entering in your callback

var _mythis = this;

and then use "_mythis" to call member function/variable


But I'm not sure it's what you need to do in your case.

Post Status

Asked in February 2016
Viewed 1,781 times
Voted 13
Answered 1 times


Leave an answer

Quote of the day: live life