2013-04-07 54 views
1

我从头开始在JQuery中创建一个photoslider,我使用setInterval()以使图像每3秒运行一次。但是,我正在使用播放/停止按钮,因为它必须等待间隔才能完成以检测按钮,所以它不响应我的需要。我可以使用另一个选项,以便播放/停止按钮立即响应?谢谢!jQuery照片滑块中setInterval()的替代方法

var test = parseInt($interval.val(),10)+1000; 
var test2 = test; 
function runInterval() { 
    $(".slider #" + count_image).show("slide", { 
     direction: "right" 
    }, 500); 
    test2 = test; 
    if ($("#parrafo").text() == "Play") { 
     $(".slider #" + count_image).delay($interval.val()).hide("slide", { 
      direction: "left" 
     }, 500); 
     if (count_image == max_images) count_image = 1; 
     else count_image = count_image + 1; 
    } 
    $interval = $("select#mySelect option:selected"); 
    test = parseInt($interval.val(), 10) + 1000; 
    clearInterval(s); 
    s = setInterval(runInterval, test2); 
} 
s = setInterval(runInterval, test2); 
+1

听起来像间隔,你的幻灯片代码太耦合了。点击按钮后,您是否可以设置一个变量来表示它已被点击,然后即使回调被触发,photoslider也不会执行任何操作。 – 2013-04-07 17:30:26

+0

这就是间隔功能: 功能runInterval() { $(+ count_image”滑块# “).show(” 滑动,{方向: “右”},500); TEST2 =测试; 如果($ (“#parrafo”)。text()==“Play”) {(“。slider#”+ count_image).delay($ interval.val())。hide(“slide”,{direction:“左 “},500); 如果(count_image == max_images) count_image = 1;否则 count_image = count_image + 1; \t \t } $间隔= $(” 选择#mySelect选项:选择“); test = parseInt($ interval.val( ),10)+ 1000; clearInterval(s); \t \t \t s = setInterval(runInterval,test2); } s = setInterval(runInterval,test2); – 2013-04-07 21:19:17

+0

请问您可以在原始问题中粘贴代码吗?如果可能,请使用http://jsbeautifier.org/等格式。 – 2013-04-07 21:23:48

回答

0

Here's a demo。我认为它工作就像你想要的。我正在使用div s而不是img s,这应该没什么区别。

HTML:

<div class="slider"> 
    <div id="0" class="img">0</div> 
    <div id="1" class="img hidden">1</div> 
    <div id="2" class="img hidden">2</div> 
    <div id="3" class="img hidden">3</div> 
    <div id="4" class="img hidden">4</div> 
</div> 
<select id="mySelect"> 
    <option>200</option> 
    <option>1000</option> 
    <option>2000</option> 
    <option>3000</option> 
</select> 
<button id="parrafo">Start/Stop</button> 
<span id="status"></span> 

CSS:

.hidden { 
    display: none; 
} 
.slider, .img { 
    width: 32px; 
    height: 32px; 
} 
.img { 
    background: blue; 
    color: white; 
    text-align: center; 
    font-size: 24px; 
} 

JS:

var slideshowActive = false; 
var slideshowTimeoutDuration = 5000; 
var slideshowTimeout = -1; 
var currentImg = 0; 
var numImgs = 5; 
var slideDuration = 500; 

function showImg(img) { 
    $(".slider #" + img).show("slide", { 
     direction: "right" 
    }, slideDuration); 
} 

function hideImg(img) { 
    $(".slider #" + img).hide("slide", { 
     direction: "left" 
    }, slideDuration); 
} 

function swapImgs(currentImg) { 
    hideImg(currentImg); 

    var nextImg = currentImg + 1; 
    if (nextImg >= numImgs) { 
     nextImg = 0; 
    } 

    // Show the next image after the currentImg has been hidden (after slideDuration). 
    setTimeout(function() { 
     showImg(nextImg); 
    }, slideDuration); 

    return nextImg; 
} 

function onSlideshowTimeout() { 
    console.log("onSlideshowTimeout", new Date()); 

    if (!slideshowActive) { 
     $("#status").html("Slideshow aborted"); 
     return; 
    } 

    currentImg = swapImgs(currentImg); 

    if (slideshowActive) { 
     // Continue the slideshow if active, by setting a new timeout. 
     slideshowTimeout = setTimeout(onSlideshowTimeout, slideshowTimeoutDuration); 
    } 
} 

$("#parrafo").click(function() { 
    // Toggle the slideshow active. 
    slideshowActive = !slideshowActive; 

    // If the slideshow is now active, start it. 
    if (slideshowActive) { 
     // Only calculate the duration when the Start button is clicked. 
     var $interval = $("select#mySelect option:selected"); 
     slideshowTimeoutDuration = parseInt($interval.val(), 10) + 1000; 
     // Start the slideshow. 
     $("#status").html("Slideshow started with duration=" + slideshowTimeoutDuration + "ms"); 
     slideshowTimeout = setTimeout(onSlideshowTimeout, slideshowTimeoutDuration); 
    } else { 
     $("#status").html("Slideshow stopped"); 
    } 
}); 

截图:

enter image description here

+0

哇!非常感谢保罗!这正是我想要的!并感谢您的评论! – 2013-04-07 22:47:03

+0

我忘了提及,因为它使用'setTimeout'而不是'setInterval',所以不需要取消超时(使用'clearTimeout')。相反,如果幻灯片显示处于活动状态,我们只会重复设置超时。 – 2013-04-07 22:49:42

+0

非常感谢Paul!现在对我来说很清楚! :) – 2013-04-08 23:11:58