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

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

    // These are three very simple and concise answers:

    function fun() {
        console.log(this.prop1, this.prop2, this.prop3);
    }
    
    let obj = { prop1: 'one', prop2: 'two', prop3: 'three' };
    
    let bound = fun.bind(obj);
    
    setTimeout(bound, 3000);
    
     // or
    
    function funOut(par1, par2, par3) {
    
      return function() { 
    
        console.log(par1, par2, par3);
    
      }
    };
    
    setTimeout(funOut('one', 'two', 'three'), 5000);
    
     // or
    
    let funny = function(a, b, c) { console.log(a, b, c); };
    
    setTimeout(funny, 2000, 'hello', 'worldly', 'people');
    

提交回复
热议问题