How can I pass a parameter to a setTimeout() callback?

前端 未结 28 1987
既然无缘
既然无缘 2020-11-21 07:31

I have some JavaScript code that looks like:

function statechangedPostQuestion()
{
  //alert("statechangedPostQuestion");
  if (xmlhttp.readyState==         


        
28条回答
  •  时光取名叫无心
    2020-11-21 07:55

    In general, if you need to pass a function as a callback with specific parameters, you can use higher order functions. This is pretty elegant with ES6:

    const someFunction = (params) => () => {
      //do whatever
    };
    
    setTimeout(someFunction(params), 1000);
    

    Or if someFunction is first order:

    setTimeout(() => someFunction(params), 1000); 
    

提交回复
热议问题