Correct use of Intervals on JS

Correct use of Intervals on JS

  

Hi,

I have a web block in my mobile application that has JS function with an interval. All works fine but on console I have lots of error messages like this:

[Error] Controller [2017-01-09T12:53:42.095Z]: Invalid call of the 'UpdateUserActivityAction1' client action of the 'MainFlow.MyScreen1' since the latter is not currently active. This is likely due to a platform's client action being used as an event handler or in a setTimeout function. Consider removing this call by using the 'On Destroy' event of the screen/block or moving your logic to a global client action.


I've tried to run window.clearInterval(Id) with the global variable id that should hopes interval id but without success.

I will apreciate any tip of how properly use js functions that run within interval on web blocks on mobile apps.

Thank you.

Solution

Hi Mykola,

The problem is that you might not be clearing the setinterval correctly.

My suggestion is that you use a local variable (instead of  global variable) in the block to store the interval Id and then clear the interval on the web-block destroy event.

Solution