0
即時嘗試創建一個計數器,問題是,即時通訊設法使計數器只有當用戶在窗口中運行,如果用戶離開窗口或另一個分隔符櫃檯應該停止,直到他回來。計數器停止出窗口瀏覽器
這裏是我的代碼:
$(window).blur(function(){
console.log("Counter Should Stop");
});
$(window).focus(function(){
window.onload = function(){
(function(){
var counter = 10;
setInterval(function() {
counter--;
if (counter >= 0) {
span = document.getElementById("count");
span.innerHTML = counter;
}
// Display 'counter' wherever you want to display it.
if (counter === 0) {
alert('this is where it happens');
clearInterval(counter);
}
}, 1000);
})();
}
});