0

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.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Elliott McNary
  • 1,149
  • 3
  • 11
  • 20

1 Answers1

1

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.

Marc
  • 1,350
  • 2
  • 11
  • 29