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

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

    Hobblin already commented this on the question, but it should be an answer really!

    Using Function.prototype.bind() is the cleanest and most flexible way to do this (with the added bonus of being able to set the this context):

    setTimeout(postinsql.bind(null, topicId), 4000);
    

    For more information see these MDN links:
    https://developer.mozilla.org/en/docs/DOM/window.setTimeout#highlighter_547041 https://developer.mozilla.org/en/docs/JavaScript/Reference/Global_Objects/Function/bind#With_setTimeout

提交回复
热议问题