当浏览器选项卡处于焦点时如何设置 setInterval 运行?
How to make setInterval run when browser tab is on focus?
我创建了一个 运行 在页面加载时每 2 秒出现一次的时间间隔。现在,当我移动到其他页面时,间隔被清除(请检查代码)。现在我想要的是当我再次移动到同一个选项卡时,间隔应该重新开始。
我尝试过的一件事是,我在 $(window).focus(//CODE)
中编写了整个代码,但问题是当页面最初在任何浏览器的选项卡中打开时,它不会 运行。
如何解决这个问题?
这是我的代码:
var zzz= setInterval(anewFunc, 2000);
function anewFunc(){
$(document).ready(function(){
var chatattr=$(".chatwindow").css("visibility");
var chattitle=$("#hideid").text();
if(chatattr=="visible"){
$.ajax({
url: 'seen1.php',
type: 'post',
data: "ctitle="+chattitle,
success: function(result9){
},
error: function(){
}
});
}
$(window).blur(function(){
$.ajax({
url: 'session.php',
type: 'post',
success: function(result10){
// alert(result10);
},
error: function(){
}
});
clearInterval(zzz);
});
});
}
One thing I tried was that I wrote this whole code inside $(window).focus(//CODE)
but the problem is that it doesn't run when the page is initially opened in any browser's tab.
好的,这里的问题是,setInterval()
没有在 0
秒执行。它仅从 2
秒开始。所以你需要做一个小改动:
- 有单独的功能。
- 在
ready
事件中,首次启动计时器,以及 运行 函数。
- 从间隔中删除事件处理程序,或仅使用
.one()
仅分配一次。您重复添加到 window 的 .blur()
事件。
更正代码:
function anewFunc() {
var chatattr = $(".chatwindow").css("visibility");
var chattitle = $("#hideid").text();
if (chatattr == "visible") {
$.ajax({
url: 'seen1.php',
type: 'post',
data: "ctitle=" + chattitle,
success: function(result9) {},
error: function() {}
});
}
$(window).one("blur", function() {
$.ajax({
url: 'session.php',
type: 'post',
success: function(result10) {
// alert(result10);
},
error: function() {}
});
clearInterval(zzz);
});
}
$(document).ready(function() {
var zzz = setInterval(anewFunc, 2000);
anewFunc();
});
Now what I want is when I move to the same tab again, the interval should start again.
您还没有再次启动 setInterval()
。
$(document).ready(function () {
$(window).one("focus", function() {
var zzz = setInterval(anewFunc, 2000);
});
});
我创建了一个 运行 在页面加载时每 2 秒出现一次的时间间隔。现在,当我移动到其他页面时,间隔被清除(请检查代码)。现在我想要的是当我再次移动到同一个选项卡时,间隔应该重新开始。
我尝试过的一件事是,我在 $(window).focus(//CODE)
中编写了整个代码,但问题是当页面最初在任何浏览器的选项卡中打开时,它不会 运行。
如何解决这个问题?
这是我的代码:
var zzz= setInterval(anewFunc, 2000);
function anewFunc(){
$(document).ready(function(){
var chatattr=$(".chatwindow").css("visibility");
var chattitle=$("#hideid").text();
if(chatattr=="visible"){
$.ajax({
url: 'seen1.php',
type: 'post',
data: "ctitle="+chattitle,
success: function(result9){
},
error: function(){
}
});
}
$(window).blur(function(){
$.ajax({
url: 'session.php',
type: 'post',
success: function(result10){
// alert(result10);
},
error: function(){
}
});
clearInterval(zzz);
});
});
}
One thing I tried was that I wrote this whole code inside
$(window).focus(//CODE)
but the problem is that it doesn't run when the page is initially opened in any browser's tab.
好的,这里的问题是,setInterval()
没有在 0
秒执行。它仅从 2
秒开始。所以你需要做一个小改动:
- 有单独的功能。
- 在
ready
事件中,首次启动计时器,以及 运行 函数。 - 从间隔中删除事件处理程序,或仅使用
.one()
仅分配一次。您重复添加到 window 的.blur()
事件。
更正代码:
function anewFunc() {
var chatattr = $(".chatwindow").css("visibility");
var chattitle = $("#hideid").text();
if (chatattr == "visible") {
$.ajax({
url: 'seen1.php',
type: 'post',
data: "ctitle=" + chattitle,
success: function(result9) {},
error: function() {}
});
}
$(window).one("blur", function() {
$.ajax({
url: 'session.php',
type: 'post',
success: function(result10) {
// alert(result10);
},
error: function() {}
});
clearInterval(zzz);
});
}
$(document).ready(function() {
var zzz = setInterval(anewFunc, 2000);
anewFunc();
});
Now what I want is when I move to the same tab again, the interval should start again.
您还没有再次启动 setInterval()
。
$(document).ready(function () {
$(window).one("focus", function() {
var zzz = setInterval(anewFunc, 2000);
});
});