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

前端 未结 28 1954
既然无缘
既然无缘 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:11

    Answering the question but by a simple addition function with 2 arguments.

    var x = 3, y = 4;
    
    setTimeout(function(arg1, arg2) { 
          delayedSum(arg1, arg2);
    }(x, y), 1000);
    
    function delayedSum(param1, param2) {
         alert(param1 + param2); // 7
    }
    

提交回复
热议问题