Are there any way to stop the running of a Javascript web application when the focus is on other window? For example, If I have AJAX executions in a application web, It'd very efficient stop the running in that situation.
Using the Page Visibility API
The Page Visibility API performs a simple but important function – it lets your application know when a page is visible to the user. This basic piece of information enables the creation of Web pages that behave differently when they are not being viewed.
Visibility.js - a wrapper for the Page Visibility API
Your question is too theoretical. There's no native way in JS for keeping track of window focus, but it's relatively simple to implement your own.
Once you know if the window has focus at a given point in time, you can use this information in your implementation that continuously fires AJAX requests (most likely in some kind of loop) and skip the firing of the request when window is not focused.
E.g.
var winFocused = false;
window.onfocus = function() {
winFocused = true;
}
window.onblur = function() {
winFocused = false;
}
Then in your "loop" or whatever, for e.g.:
setInterval(function() {
if( ! winFocused) return;
// Otherwise, if winFocused is true, do what you need...
}, 1000);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With