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

前端 未结 28 2318
既然无缘
既然无缘 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:07
    setTimeout(function() {
        postinsql(topicId);
    }, 4000)
    

    You need to feed an anonymous function as a parameter instead of a string, the latter method shouldn't even work per the ECMAScript specification but browsers are just lenient. This is the proper solution, don't ever rely on passing a string as a 'function' when using setTimeout() or setInterval(), it's slower because it has to be evaluated and it just isn't right.

    UPDATE:

    As Hobblin said in his comments to the question, now you can pass arguments to the function inside setTimeout using Function.prototype.bind().

    Example:

    setTimeout(postinsql.bind(null, topicId), 4000);
    
    0 讨论(0)
  • 2020-11-21 08:07

    You can pass the parameter to the setTimeout callback function as:

    setTimeout(function, milliseconds, param1, param2, ...)

    eg.

    function myFunction() {
      setTimeout(alertMsg, 3000, "Hello");
    }
    
    function alertMsg(message) {
        alert(message)
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2020-11-21 08:12

    Note that the reason topicId was "not defined" per the error message is that it existed as a local variable when the setTimeout was executed, but not when the delayed call to postinsql happened. Variable lifetime is especially important to pay attention to, especially when trying something like passing "this" as an object reference.

    I heard that you can pass topicId as a third parameter to the setTimeout function. Not much detail is given but I got enough information to get it to work, and it's successful in Safari. I don't know what they mean about the "millisecond error" though. Check it out here:

    http://www.howtocreate.co.uk/tutorials/javascript/timers

    0 讨论(0)
  • 2020-11-21 08:12

    You have to remove quotes from your setTimeOut function call like this:

    setTimeout(postinsql(topicId),4000);
    
    0 讨论(0)
提交回复
热议问题