google chrome - Detecting if browser window is active and start event after window is active again [JavaScript] -
the idea simple: have buttons refer website. whenever user has clicked more 2 links want refresh content (through ajax). work need detect if window active or not, since want event start when user on page.
without further ado code:
$(document).on("click", "a", function() { numberofclicks += 1; if (numberofclicks >= 2) { useronwebsiteornot(); numberofclicks = 0; } }); function useronwebsiteornot() { if (focusedornot == 0) { $('#resultaat').hide().fadein(5000); } } window.addeventlistener('focus', function() { document.title = 'focused'; focusedornot = 0; }); window.addeventlistener('blur', function() { document.title = 'not focused'; focusedornot = 1; }); it detect whenever user on page or not, somehow fade happens. explain me i'm doing wrong or give me ideas?
thanks yenthe
answer: needed settimeout on 3 functions because otherwise check fast. thank romo! ;) credit goes romo honest.
$(document).on("click", "a", function() { numberofclicks += 1; if (numberofclicks >= 2) { haallinks(); settimeout(function() { useronwebsiteornot(); }, 2000); numberofclicks = 0; } }); function useronwebsiteornot() { if (focusedornot === 0) { $('#resultaat').hide().fadein(5000); } else { controlerenactiefofniet(); } } function controlerenactiefofniet() { settimeout(function() { useronwebsiteornot(); }, 2000); } window.addeventlistener('focus', function() { settimeout(function() { focusedornot = 0; }, 0); }); window.addeventlistener('blur', function() { settimeout(function() { focusedornot = 1; }, 1000); });
i think problem when click link second time window focused. js runs pretty fast. overcome this, think should settimeout() , delay 200ms or give window time "lose" focus
settimeout(function() {useronwebsiteornot(); },2000); edit: adding delay event listener. don't think can "delay" event though, function runs.
window.addeventlistener('focus', function() { settimeout( function() { $('#test').html('focus'); focusedornot = 0; } , 5000); });
Comments
Post a Comment