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

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

I have some JavaScript code that looks like:

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


        
28条回答
  •  一整个雨季
    2020-11-21 08:08

    After doing some research and testing, the only correct implementation is:

    setTimeout(yourFunctionReference, 4000, param1, param2, paramN);
    

    setTimeout will pass all extra parameters to your function so they can be processed there.

    The anonymous function can work for very basic stuff, but within instance of a object where you have to use "this", there is no way to make it work. Any anonymous function will change "this" to point to window, so you will lose your object reference.

提交回复
热议问题