Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setTimeout calling function inside function - Scope-Issue

So, the problem is that I have a function inside a function that needs to be called by setTimeout. That doesn't work however because setTimeout will assume that the function it calls has the root as its' scope.

Any idea how I could solve this without changing the scope of the function?

Edit:

Here is what I mean:

function general(){
    function saysomething(){
        console.log('hi there');
    }
setTimeout("saysomething();", 1000);
}

The setTimeout fails..

like image 408
Julian Avatar asked Dec 28 '25 15:12

Julian


2 Answers

function general(){
    function saysomething(){
        console.log('hi there');
    }
    setTimeout(saysomething, 1000);
}
like image 132
Ben Avatar answered Dec 31 '25 05:12

Ben


Not positive this is what you mean but you can pass the variables when you call the function in the setTimeout

function f1(){
    var a='1';
    var b='b';
    setTimeout(function(){f2(a,b);},1000)

}

function f2(a,b){
      alert(a + b);
}

f1();
like image 42
Johnny Craig Avatar answered Dec 31 '25 05:12

Johnny Craig



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!