Context: There is an old jquery / php project. There is a main page where there is a list of users online. Once in n-seconds (in my case 5), an ajax request is twitching, which gets the html code from the view-shy and inserts it into a div (just to make it clear:
(function update() { js.ajax({ type: "POST", url: '/index.php?task=getListUsers&lastUserLogin=<?=filter_input(INPUT_COOKIE, 'lastUserLogin', FILTER_VALIDATE_INT)?>', data: '', success: function(data){ js('.right-column').html(data); js('.top_block0 .jPushMenuBtn').show(); //console.log(data); updateNumberUser(); } }).then(function() {// on completion, restart setTimeout(update, right_column_update_time);// function refers to itself }); })(); The page is often folded in the tab. ajax twitches until a certain point successfully but then falls off (error 500 in the console). What I've done:
- determined that the tab is minimized or active;
- I write in the cookie status;
- If the tab has become active, I launch ajax, otherwise I won't pull.
And this all works however after a certain period of time falls off and you have to reload the page.
var vis = (function(){ var stateKey, eventKey, keys = { hidden: "visibilitychange", webkitHidden: "webkitvisibilitychange", mozHidden: "mozvisibilitychange", msHidden: "msvisibilitychange" }; for (stateKey in keys) { if (stateKey in document) { eventKey = keys[stateKey]; break; } } return function(c) { if (c) { document.addEventListener(eventKey, c); //document.addEventListener("blur", c); //document.addEventListener("focus", c); } return !document[stateKey]; } })(); var status; vis(function(){ status = vis() ? '1' : '0'; console.log(new Date, 'visible ?', vis()); }); // to set the initial state status = vis() ? '1' : '0'; js.cookie('ajax_status', status, {expires: 365, path: '/'}); var ajax_status=js.cookie('ajax_status'); if(ajax_status=='1'){ //функция выше } How to make the page work and not fall off? Perhaps the code requires fundamental processing and in general getting rid of this heresy. I ask for help / advice from a JS guru)) Vorkers came to mind not to tug at the DOM once again, but I still didn’t "feel" them in practice.