– Juan Gabriel Covas. 2012
El objetivo es recargar la página al cabo de cierto tiempo, siempre que el usuario no esté navegando por ella (si mueve el ratón o pulsa alguna tecla, estando en la página -es decir tiene el foco- se resetea el contador y NO se recarga).
ElTiempo.modules.PageReloader = { init: function() { var _this = this; // Increment the idle time counter every freakin' second eltiempo_data.reload_pageInterval = setInterval(_this.page_reload_check, 1000); // 1 second // Setup Zeroing the idle timer on mouse or keypress movement. $(document).mousemove(function (e) { reload_page_idleTime = 0; }); $(document).keypress(function (e) { reload_page_idleTime = 0; }); }, page_reload_check: function() { // Enough time has ellapsed if (reload_page_idleTime > PAGE_RELOAD_SECONDS - 1) { // configured seconds - 1 // do NOT reload until document is really focused var focused = document.activeElement; if (!focused || focused == document.body) focused = null; else if (document.querySelector) focused = document.querySelector(":focus"); if (! focused) { return false; } reload_page_idleTime = 0; window.location.reload(); return true; } else { reload_page_idleTime = reload_page_idleTime + 1; } if (eltiempo_data.env == 'test' || eltiempo_data.env == 'local') { $('#proxima-actualizacion span#page_reload_box').html(' Recarga en '+(PAGE_RELOAD_SECONDS - reload_page_idleTime + 1)+' '); } } };
~~DISCUSSION|Comentarios~~