Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this setTimeout() methods not working in this function?

Tags:

javascript

function SlideShow(area)
    {
        var SlideImg = new Array('img1', 'img2');
        var SlideArea = document.getElementById(area);
        for(i=0;i<SlideImg.length;i++)
        {
            var html = '<img src="images/room/' + SlideImg[i] + '.jpg" id="' + SlideImg[i] + '" class="not-active" />';
            SlideArea.innerHTML += html;
        }
        var a = 0;
        function RunSlide()
        {
            document.getElementById(SlideImg[a]).className = 'active';
            a++;    
        }
        var run = setTimeout('RunSlide()', 5000);
    }

This function not working after I add the setTimeout() method there. Can anybody help me?

like image 211
Ega Rana Avatar asked Jul 30 '26 19:07

Ega Rana


1 Answers

Just change it to:

var run = setTimeout(RunSlide, 5000);

The reason is: when you pass a string to setTimeout() it is evaluated in global context - where RunSlide is not visible, because it is local.

Passing a string to setTimeout() is never a good idea, here you have one reason.

like image 200
Tomasz Nurkiewicz Avatar answered Aug 02 '26 10:08

Tomasz Nurkiewicz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!