【问题标题】:How to resue setInterval function after clearing it? Only javascript please清除后如何重用setInterval函数?请只使用javascript
【发布时间】:2015-07-05 08:08:43
【问题描述】:

Onmouseover(在图像上)我正在清除 setInterval 函数。 Onmouseout 我需要重用相同的 setInterval 函数。但是由于它在鼠标悬停事件期间被清除,因此它不适用于鼠标悬停。如何再次使用 setinterval 函数 onmouseout?

我正在尝试创建一个幻灯片小部件(类似于 YAHOO HOMEPAGE 中的东西)。

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);

    });
}

【问题讨论】:

  • 能贴一下相关代码吗?
  • 你可以想象我正在尝试在雅虎主页中创建什么。我无法粘贴所有 HTML 代码,因为一切都很长。这是特定的 JS 代码,我在这里感到震惊

标签: javascript


【解决方案1】:

在函数中创建间隔,这样在鼠标退出时你可以重新运行这个函数。

另一种方法是让区间保持运行,但更新一个变量以阻止区间函数完全执行。像这样的:

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

}

【讨论】:

    猜你喜欢
    • 2012-08-19
    • 1970-01-01
    • 2012-08-23
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    相关资源
    最近更新 更多