I need a timer to run in a Custom widget to carry out an action periodically on one of the controls:
I have been advised to create the timer in afterRender: and destroy it in willUnmount: If I monitor the lifecycle of using the SUrvey Creator to place a widget, move between the tabs"Survey Designer" and "Test Survey" then all looks good.
This is probably a newbie issue but I am having difficulty setting the scope for the timer so that the timer created in afterRender can be destroyed in the different will Unmount function.
If I create the timer thus in afterRender:
CodemapRefreshTimer = setInterval(
() => {
console.log("Do stuff");
},
1000);
Then when I go to willUnmount then the function does not have access to the scope of the timer to run clearInterval.
I tried to define the var at the "function init(Survey: any)" level and altered the code thus;
Codethis.mapRefreshTimer = setInterval(
() => {
console.log("Do stuff");
},
1000);
and in willUnmount:
CodeclearInterval(this.mapRefreshTimer);
This seemed to work but a timer remains running when the widget is deleted completely as a question. I presume I am scoping the timer incorrectly? Can you give me a pointer please?
Thanks,
Paul