Issue
I defined a function called func1 which works fine when called normally. However, when I passed it as a callback to setInterval, it could not access the variable outside the function. This issue is present in nodejs, but I am not sure if it is a bug or my mistake. I received an error message about not being able to access outer functions when trying to use clearInterval in setInterval's callback. A temporary workaround suggested by appsmith was to use appsmith store.
Resolution
The issue is that a function passed as a callback to setInterval
or other functions may not be able to access variables defined outside of it. One possible workaround is to use the Appsmith store to store the variables and access them from the callback function. Here's an example of how to do this:
(function() {
const store = Symbol();
const func1 = function() {
let v1 = Appsmith.store[store].v1;
console.log(v1);
};
Appsmith.store[store] = { v1: 0 };
setInterval(func1, 1000, 'test');
})();
In this example, the variable v1
is stored in the Appsmith store using a symbol as the key. The func1
callback function retrieves the value of v1
from the store and logs it. The setInterval method is passed func1
as a callback and runs it every second. This allows func1
to access v1
even though it is defined outside of the function.