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,
     easing:'linear',
     step: function() {
      var counter = Math.floor(this.countNum)
      console.log(counter)
     },
     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.

Answers


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

_mythis.myJQfct();

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

Search




Leave an answer