Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript help: Simple script won't work when calling setTimeout

Tags:

javascript

I want to change the exist in a span element every 2 seconds, but it showing only what is in case 0: 'asd'.

Could anyone tell me why this is not working?

var n = 0;

function hideVideoSpan(type){
    switch(type)
    {
        case 0:
        {
            $("#hideVideoSpan").html('asd');
            n = 1;
            break;
        }

        case 1:
        {
            $("#hideVideoSpan").html('lol');
            n = 0;
            break;
        }

        default:break;
    }

    setTimeout(hideVideoSpan(n), 2000);
}

hideVideoSpan(n);
like image 831
Reteras Remus Avatar asked Aug 14 '26 09:08

Reteras Remus


1 Answers

You are calling the function, not making a reference

setTimeout(hideVideoSpan(n), 2000);

Needs to be a closure

setTimeout( function(){ hideVideoSpan(n); }, 2000);
like image 87
epascarello Avatar answered Aug 16 '26 23:08

epascarello



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!