清除setInterval函数后如何恢复?请只使用javascript

How to resue setInterval function after clearing it? Only javascript please

本文关键字:javascript 恢复 函数 setInterval 何恢复 清除      更新时间:2023-09-26

Onmouseover(在图像上(我正在清除setInterval函数。在mouseout中,我需要重用相同的setInterval函数。但由于它在mouseover事件中被清除,因此在mouseout上不起作用。如何在mouseout上再次使用setinterval函数?

我正在尝试创建一个幻灯片小部件(类似于YAHOO主页(。

p = 0;
function list(event) {
    var x = event.target || event.srcElement;
    document.getElementById("outer").src = x.src;
    x.style.borderBottom = "thick solid #0000FF";
    allimg[0].getElementsByTagName('img')[g].style.borderBottom = "none";
    clearInterval(cha); //clearing autoincrement of images onmouseover
    clearInterval(rcli); //clearing autoright click button onmouseover
    clearInterval(chanid); //??? CLEARING HERE 
    x.addEventListener('mouseout', function () {
        x.style.borderBottom = "none";
        function chan() {
            allimg = document.getElementsByClassName("container"); //container within which all images are there
            u = allimg[0].getElementsByTagName('img');
            for (p; p < u.length; p++) {
                if (u[p].style.borderBottomStyle == "solid" || u[p] == x) {
                    a = p;
                    u[a].style.borderBottom = "none";
                    a++;
                    document.getElementById("outer").src = u[a].src;
                    u[a].style.borderBottom = "thick solid #0000FF";
                    break;
                }
            }
            p++;
        }
        chanid = setInterval(chan, 2000);
    });
}

在函数中创建间隔,这样在mouseout中就可以重新运行此函数。

另一种方法是保持间隔运行,但更新一个变量,使间隔函数无法完全执行。类似这样的东西:

var paused;
setInterval(onInterval, 1000);
$img.on("mouseover", function () {
    // on mouseover 'pause' the interval
    paused = true;
})
.on("mouseout", function () {
    // on mouseout 'unpause' it
    paused = false;
});
function onInterval () {
    // if paused do nothing
    if(paused) return;
    // do regular interval stuff here
}