简体   繁体   English

Jquery 简单的 slider 问题,当浏览器选项卡不活动时

[英]Jquery simple slider problem when browser tab not active

I wrote a simple jquery slider that basically goes through 3 divs, hidding one and then fadeIn the next and so on using setInterval()我写了一个简单的 jquery slider 基本上经过3个div,隐藏一个然后fadeIn下一个等等使用setInterval()

The slider works just fine for my purposes, but when I open other tabs and then come back to the page's tab, all the divs are visible and then they start to disappear and it starts working again. slider 对于我的目的来说工作得很好,但是当我打开其他选项卡然后回到页面的选项卡时,所有的 div 都是可见的,然后它们开始消失,它又开始工作了。

Here is my jquery, which is inside $(function(){}) :这是我的 jquery,在$(function(){})里面:

    $('#slideshow-next').click(function() {
    pauseSlideshow();
    nextSlide();
});

$('#slideshow-prev').click(function() {
    pauseSlideshow();
    prevSlide();
});

$('#slideshow-pause').click(function(){
    pauseSlideshow();
});

$('#slideshow-play').click(function() {
    playSlideshow();
});

interval = setInterval('nextSlide()', 4000);
});

function playSlideshow() {
    interval = setInterval('nextSlide()', 4000);
    $('#slideshow-play').hide();
    $('#slideshow-pause').show();
    nextSlide();    
}

function pauseSlideshow() {
    interval = clearInterval(interval);
    $('#slideshow-pause').hide();
    $('#slideshow-play').show();
}

function nextSlide() {

    //hide current slide
    $('#slide'+currentSlide).hide();
    // show next slide
    var next = (currentSlide+1)%3;
    $('#slide'+next).fadeIn('slow');
    currentSlide = next;        
}

function prevSlide() {

    //hide current slide
    $('#slide'+currentSlide).hide();
    // show next slide
    var next = (currentSlide-1)%3;
    $('#slide'+next).fadeIn();
    currentSlide = next;
}

I think you might be hitting a different case as asked on Chrome: timeouts/interval suspended in background tabs?我认为您可能会遇到Chrome 上询问的不同情况:后台选项卡中暂停的超时/间隔? , which states that: ,其中指出:

When a tab is inactive, only at a maximum of once per second the function is called.当标签不活跃时,只有每秒最多一次,function被称为。

So you could either code the setInterval differently How can I make setInterval also work when a tab is inactive in Chrome?因此,您可以对setInterval进行不同的编码。 当 Chrome 中的选项卡处于非活动状态时,如何使 setInterval 也可以工作? or detect the tab switch to halt the slideshow and re-start on tab focus How to tell if browser/tab is active或检测选项卡开关以停止幻灯片并在选项卡焦点上重新开始如何判断浏览器/选项卡是否处于活动状态

Also, Jquery setInterval too fast when coming from another tab may be useful as it states that此外, 当来自另一个选项卡时,Jquery setInterval too fast可能很有用,因为它指出

Firefox 5+ also clamps to one timeout per second in inactive tabs. Firefox 5+还在非活动选项卡中限制为每秒一次超时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM