本文介绍了我如何在jQuery中的函数之外调用clearInterval? setInterval之外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    function iPadMovie(id) {
    $(function () {
        var i = 1;
        var interval = setInterval(function () {
            jQuery('.animationMax img').attr({
                src: 'http://jdsports.scene7.com/is/image/JDSports/127932jd' + ('0' + i).slice(-2) + '?hei=255&wid=427&resmode=sharp&op_usm=1.1,0.5,0,0&defaultImage=JDSports/sizeImageMissing'
            });
            i++;
            if (i === 28) i = 1;
        }, 100);
    });
}

function playIpad(){
    iPadMovie();
}


function stopIpad(){
    clearInterval = interval;
}

您可以在这里看到小提琴:我希望能够停止播放电影并重新启动它。当然我可以在方法之外使用clearInterval?

You can see the fiddle here: http://jsfiddle.net/Vv2u3/15/ I want to be able to stop the movie and restart it if they press play. Surely I can use clearInterval outside the method?

推荐答案

下面是示例。

Little bit explanation here.First of all, your interval variable, (which is actual handler for returned callback function by setInterval) is not visible outside of iPadMovie() function so interval variable should be declared outside of this function.Second you should call clearInterval(handler) function inside of stopIpad() function. More information can be founded here.

function stopIpad(){
    clearInterval(interval);
}

这篇关于我如何在jQuery中的函数之外调用clearInterval? setInterval之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-16 04:05