How do I use setInterval to call a JS Object?

I have the following code that I’m trying to display and refresh every second in a text widget:

{{setInterval(()=>{timesheet_process.myFun2.data }, 1000);}}

It doesn’t want to run at all, let alone every second. It is an async function, and runs just fine statically when set as:

{{timesheet_process.myFun2.data}}

Is there a way to use setInterval with JS Objects? Thanks!

@drew You can use setInterval to run JSObjects. The thing is in your code above you are binding the data from the function instead of running the function at set intervals - timesheet_process.myFun2(). Also the setInterval would need something to trigger it. Either the Interval function should run at page load or using a switch widget.

You can check out this video tutorial- How To Use The SetInterval & ClearInterval Functions to auto-update widgets - YouTube

Feel free to reach out in case of any queries.

@dancia Thanks for the help, I’ve gone ahead and emulated the tutorial including the switch. Still no dice. Am I missing something somewhere else? I have the following in the text widget.

{{(()=>{
    const id = 'autoupdate';
    if(Switch1.isSwitchedOn) {
        setInterval(() =>
        timesheet_process.myFun2(), 2000, id);
    } else {
        clearInterval(id);
    }
})()}}

If it matters, I’m returning a moment.js object, example: 11:56:38, code below:

export default {

	myFun2: async () => {
			let res = await time_entry_single_get.run();
			const start_time_raw = res[0].start_time;
			const m2 = moment(start_time_raw);
			const m1 = moment();
			const duration = moment.duration(m1.diff(m2));
 			let value = moment.utc(moment.duration(duration, "seconds").asMilliseconds()).format("HH:mm:ss");
			return value;
			}
}

@drew The text widget should contain only the binding - {{timesheet_process.myFun2.data}}

and you should add the set interval code snippet on the onChange event of the Switch widget so that the function is executing when the switch is on