如何退出 jquery 中定义的 setInterval 函数

发布于 2024-09-18 18:06:53 字数 804 浏览 2 评论 0原文

我有这个 JavaScript 代码:

        var step = 8;                 // How many pixels to move per step
        var current = -1920;            // The current pixel row
        var imageWidth = 1920;        // Background image width
        var headerWidth = 960;        // How wide the header is.

        function slide_in(){
            //Go to next pixel row.
            current += step;

            //Set the CSS of the header.
            $('#bgfade').css("background-position",current+"px 0");

            if(current==0) alert('stop');
        }

        //Calls the scrolling function repeatedly
        $('#bgfade').click(function(){        
            var init = setInterval("slide_in()", 1);
        });

这使得背景幻灯片。我想在 var current = 0 时退出,并让背景处于该位置。

谢谢

i have this javascript code:

        var step = 8;                 // How many pixels to move per step
        var current = -1920;            // The current pixel row
        var imageWidth = 1920;        // Background image width
        var headerWidth = 960;        // How wide the header is.

        function slide_in(){
            //Go to next pixel row.
            current += step;

            //Set the CSS of the header.
            $('#bgfade').css("background-position",current+"px 0");

            if(current==0) alert('stop');
        }

        //Calls the scrolling function repeatedly
        $('#bgfade').click(function(){        
            var init = setInterval("slide_in()", 1);
        });

this makes the background slide. i want to exit when the var current is = 0, and let the background in that position.

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

來不及說愛妳 2024-09-25 18:06:53

您需要使用 clearInterval(),例如this:

var step = 8;                 // How many pixels to move per step
var current = -1920;            // The current pixel row
var imageWidth = 1920;        // Background image width
var headerWidth = 960;        // How wide the header is.
var init;

function slide_in(){
    //Go to next pixel row.
    current += step;

    //Set the CSS of the header.
    $('#bgfade').css("background-position",current+"px 0");

    if(current==0) clearInterval(init);
}

//Calls the scrolling function repeatedly
$('#bgfade').click(function(){        
    init = setInterval(slide_in, 1);
});

这里的其他更改是范围,因此计时器 ID 可用于其他函数,并且不将字符串传递给 setInterval() 以避免任何其他潜在问题。

You need to use clearInterval(), like this:

var step = 8;                 // How many pixels to move per step
var current = -1920;            // The current pixel row
var imageWidth = 1920;        // Background image width
var headerWidth = 960;        // How wide the header is.
var init;

function slide_in(){
    //Go to next pixel row.
    current += step;

    //Set the CSS of the header.
    $('#bgfade').css("background-position",current+"px 0");

    if(current==0) clearInterval(init);
}

//Calls the scrolling function repeatedly
$('#bgfade').click(function(){        
    init = setInterval(slide_in, 1);
});

The other changes here are the scope, so the timer ID is available to the other function, as well as not passing a string to setInterval() to avoid any other potential issues.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文