var msOnPage = 0; var intervalID = 0; function startTimeOnPage() { intervalID = setInterval( function() { msOnPage += 500; if( 0 == msOnPage % 3000 ) { $cmhp_jq.ajax( '/eh-content/stat.ajax.php?user=4844&landingpage=&page=248459&nocache=1519124985&ajax=followup' ); } }, 500 ); } function pauseTimeOnPage() { clearInterval( intervalID ); } 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); return !document[stateKey]; } })(); $cmhp_jq( document ).ready( function() { if( vis() ) { $cmhp_jq.ajax( '/eh-content/stat.ajax.php?user=4844&landingpage=&page=248459&nocache=1519124985&ajax=initial' ); startTimeOnPage(); } vis( function() { if( vis() ) { startTimeOnPage(); } else { pauseTimeOnPage(); } }); });