如何退出 jquery 中定义的 setInterval 函数
我有这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
clearInterval()
,例如this:这里的其他更改是范围,因此计时器 ID 可用于其他函数,并且不将字符串传递给
setInterval()
以避免任何其他潜在问题。You need to use
clearInterval()
, like this: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.