setTimeOut Arrgument Passing

后端 未结 2 451
一整个雨季
一整个雨季 2021-01-21 14:10

In JavaScript I want to use the setTimeOut() function like this



        
2条回答
  •  说谎
    说谎 (楼主)
    2021-01-21 14:24

    If you want to use parameters, try this example:

    var x = "OK";
    setTimeout(alertOK.bind(null,x), 3000);
    x = "Would be WRONG";
    console.log("before timeout:", x);
    
    function alertOK(x){
    	console.log("after timeout:",x);
    }

    It also works when in loop (where creating functions is not advisable).

提交回复
热议问题