I have something like this in frontend Javascript:
function myfunc() {
for (var i=0; i<=1000000000; i++)
// Do something
return data;
}
I want to call myfunc()
in an async way so that I can pass a callback instead of waiting for the loop to finish.
Is there a way to do this? I can use with or without AngularJS
/ jQuery
UPDATE 1:
What in "Do something" part is just basic math computation. It doesn't take much time to do one iteration but because the number of loop, this is blocking the browser.
There is no ajax request and I plan not to do this on Node.js server side.
UPDATE 2:
I just want to be more specific on the "Do something" part. It is canvas processing. See
https://github.com/girliemac/Filterous/blob/master/src/filterous.js#L73
for (var i = 0; i < d.length; i += 4) {
var r = d[i];
var g = d[i + 1];
var b = d[i + 2];
// CIE 1931 luminance
var avg = 0.2126*r + 0.7152*g + 0.0722*b;
// d is a reference to pixels.data, so you do not need to reassign it
d[i] = d[i + 1] = d[i + 2] = avg
}
Maybe there is a way to make this async manner so it doesn't freeze the browser or iOS Safari for like 2 seconds. It's very noticeable on mobile.