proefkip proefkip - 3 months ago 18
Javascript Question

Unable to Clearinterval after set?

I created a slideshow(

function slide()
):
I want this function to start when click on 'play'
and pause when click on 'pause'.
I used setinterval it works, only clearinterval refuse to work, what am I doing wrong?

<div id='play' onclick='play()'><img src='image1/fleche.png'></div>
<div id='pause' onclick='stop()'><img src='image1/break.png'></div>




function play(){
var test = setInterval(slide,500);}
function stop(){
clearInterval(test);}



Answer

It is because "test" is undefined inside the stop function. For your code to work, you can make the "test" variable global, like this:-

var test = null;

function play(){
    test = setInterval(slide,500);}
function stop(){
    clearInterval(test);}
Comments