I am using the following script to display jpg images as an animation:
var interval;
function Play() {
setInterval(Playimages, 100);
}
function Stop() {
alert("stop");
clearInterval(interval);
}
function Playimages() {
i = (i < sl - 1) ? (i + 1) : 0;
$('#Image1').attr('src', images[i].src);
}
Play works well, however I can not stop or pause; there is a mistake in my clearInterval. Would appreciate your suggestions. Thanks
I am using the following script to display jpg images as an animation:
var interval;
function Play() {
setInterval(Playimages, 100);
}
function Stop() {
alert("stop");
clearInterval(interval);
}
function Playimages() {
i = (i < sl - 1) ? (i + 1) : 0;
$('#Image1').attr('src', images[i].src);
}
Play works well, however I can not stop or pause; there is a mistake in my clearInterval. Would appreciate your suggestions. Thanks
Share Improve this question edited Aug 30, 2012 at 22:50 Gert Grenander 17.1k6 gold badges41 silver badges43 bronze badges asked Aug 30, 2012 at 22:34 hnclhncl 2,3057 gold badges63 silver badges132 bronze badges 04 Answers
Reset to default 6You never assign the interval id to interval
. Should be
interval = setInterval(Playimages, 100);
You need to change
setInterval(Playimages, 100);
to
interval = setInterval(Playimages, 100);
setInterval
returns an id which can be used with clearInterval
. In your code interval
is probably undefined, so clearInterval
does nothing because it has no reference.
You need to assign the interval id to interval
.
var interval;
function Play() {
interval = setInterval(Playimages, 100);
}
You have to assign the interval variable in setInterval
interval = setInterval(Playimages, 100);
clearInterval(interval);