jQuery unbinding toggle on setinterval函数

时间:2014-02-07 19:44:56

标签: jquery toggle

我试图取消绑定一个每2秒在两个html元素之间切换/切换的函数,当moused over the function应该停止,这样人们可以根据需要看到该元素。

我稍后会添加绑定函数以恢复mouseout上的切换。

$(document).ready(function () {
    function switchBox() {
        $('#imagebox7img').toggle('slow', 'linear');
        $('#map-canvas').toggle('slow', 'linear');
    }
    // hide image
    $('#imagebox7img').hide();
    // set timer and function
    setInterval(switchBox, 2000);
    // disable function on hover
    $('#imagebox7img').mouseover(function () {
        $(this).unbind();
    });
}); // end ready

1 个答案:

答案 0 :(得分:1)

setInterval会返回唯一的时间间隔,如果您可以转到clearInterval取消重复操作。

    // set timer and function
    var interval = setInterval(switchBox, 2000);
    $('#imagebox7img').mouseover(function () {
        clearInterval(interval )
    });