Take this function for example:
function mySlowFunction(baseNumber) {
console.time('mySlowFunction');
var result = 0;
for (var i = Math.pow(baseNumber, 10); i >= 0; i--) {
result += Math.atan(i) * Math.tan(i);
};
console.timeEnd('mySlowFunction');
return result;
}
This is a very CPU-intensive function. It takes around 3 seconds to complete for mySlowFunction(5)
.
Now consider this code:
setInterval(() => console.log("interval"), 1000);
mySlowFunction(5);
I expect it to log "interval" every second, but I understand that since mySlowFunction
is running on the thread and JS is single-threaded, the callback for setInterval
is executed only after mySlowFunction
completes.
Is there any way I can ensure the callback is called on the specified interval? I want this in a browser-based environment.