Making ajax call upon activation?

Hey, I just noticed something on facebook.com, facebook was on my other tab and I looked at SO, after a while I switched to facebook.com and it seems to disappear in the new news in the wall. How did they create a script that knows when you are active again, without clicking anywhere, should it update new messages, etc.? Just like in facebook chat, if you have it on another tab / window, you will hear “blub” or Facebook: ** NEW MESSAGE FROM: name. How does it do this?

Perform a timeout check every second, I don’t think so ?, it will be a lot of traffic and server response time if 3-4-500 million users do it every day.

Can someone provide an example where to place an ajax call, if he left the focus and focus again, how to make code that starts the ajax call? Jquery

+3
source share
1 answer

Would this work with a focal event on a window object? those.:

window.onfocus = refreshStuff;

where refreshStuff is your function to update ... err ... stuff.

https://developer.mozilla.org/en/DOM/window
http://msdn.microsoft.com/en-us/library/ms536934(v=VS.85).aspx

Sorry - I would add this to the comments, but I can not - perhaps because I do not yet have a sufficient reputation.

+1
source

Source: https://habr.com/ru/post/1762067/


All Articles