admin管理员组文章数量:1278980
function iPadMovie(id) {
$(function () {
var i = 1;
var interval = setInterval(function () {
jQuery('.animationMax img').attr({
src: '' + ('0' + i).slice(-2) + '?hei=255&wid=427&resmode=sharp&op_usm=1.1,0.5,0,0&defaultImage=JDSports/sizeImageMissing'
});
i++;
if (i === 28) i = 1;
}, 100);
});
}
function playIpad(){
iPadMovie();
}
function stopIpad(){
clearInterval = interval;
}
You can see the fiddle here: / I want to be able to stop the movie and restart it if they press play. Surely I can use clearInterval outside the method?
function iPadMovie(id) {
$(function () {
var i = 1;
var interval = setInterval(function () {
jQuery('.animationMax img').attr({
src: 'http://jdsports.scene7./is/image/JDSports/127932jd' + ('0' + i).slice(-2) + '?hei=255&wid=427&resmode=sharp&op_usm=1.1,0.5,0,0&defaultImage=JDSports/sizeImageMissing'
});
i++;
if (i === 28) i = 1;
}, 100);
});
}
function playIpad(){
iPadMovie();
}
function stopIpad(){
clearInterval = interval;
}
You can see the fiddle here: http://jsfiddle/Vv2u3/15/ I want to be able to stop the movie and restart it if they press play. Surely I can use clearInterval outside the method?
Share Improve this question asked Dec 23, 2013 at 14:44 TheBlackBenzKidTheBlackBenzKid 27.1k45 gold badges141 silver badges214 bronze badges 5- Try calling the function instead of overwriting it ;-) – John Dvorak Commented Dec 23, 2013 at 14:45
- @JanDvorak can you show example? – TheBlackBenzKid Commented Dec 23, 2013 at 14:47
-
Your
interval
variable is scoped to the anonymous function passed to jQuery's DOM ready handler, so won't be visible in thestopIpad()
function. After you've fixed that, it's just:clearInterval(interval)
rather thanclearInterval = interval
. – Anthony Grist Commented Dec 23, 2013 at 14:47 - define interval variable on the outerscope (global???) – A. Wolff Commented Dec 23, 2013 at 14:47
-
clearInterval(interval)
– John Dvorak Commented Dec 23, 2013 at 14:47
2 Answers
Reset to default 9Here is example link.
var interval;
function iPadMovie(id) {
$(function () {
var i = 1;
interval = setInterval(function () {
jQuery('.animationMax img').attr({
src: 'http://jdsports.scene7./is/image/JDSports/127932jd' + ('0' + i).slice(-2) + '?hei=255&wid=427&resmode=sharp&op_usm=1.1,0.5,0,0&defaultImage=JDSports/sizeImageMissing'
});
i++;
if (i === 28) i = 1;
}, 100);
});
}
function playIpad(){
iPadMovie();
}
Little bit explanation here.
First of all, your interval variable, (which is actual handler for returned callback function by setInterval) is not visible outside of iPadMovie()
function so interval variable should be declared outside of this function.
Second you should call clearInterval(handler)
function inside of stopIpad()
function. More information can be founded here.
function stopIpad(){
clearInterval(interval);
}
clearInterval
is a function. You should call it, passing it the interval you want to clear as the only argument:
function stopIpad(){
clearIntervar(interval);
}
This part is almost correct, interval
is a variable where you remember the interval handle that you want to clear:
var interval = setInterval(...);
However, the interval
variable needs to be declared outside the iPadMovie
function so that it's visible to the stopIpad
function. Move the var
statement outside.
Also, what happens if the play button is pressed twice? You could stop the current interval before you set a new one:
var interval;
function iPadMovie(){
clearInterval(interval);
interval = setInterval(...);
}
As a side note, is there a reason why the iPadMovie
waits for document ready? I would expect it to only be called after that point. If this is the case, you can remove the $(function(){...})
wrapper.
本文标签:
版权声明:本文标题:javascript - How can I call clearInterval outside of a function in jQuery? Outside the setInterval - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741216506a2360146.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论