Javascript 函数返回 value.根据第一个函数返回值调用另一个函数

Javascript function returns value.call another function according to first functions return value

本文关键字:函数 返回 返回值 值调用 另一个 第一个 value Javascript      更新时间:2023-09-26

抱歉,这可能不是新问题,但我是JS的新手,我没有得到任何具体的例子。

function find(param){            
        $.each(array,function(i,elem){              
            if(elem.id==param){
                return i;
            }
        })
        return null;
    }
//after calling this function i want to perform actions based on result
$(document).on("click","#elem",function(){
      //want to make this part synchronous
      $.when(i=find(param1)).then(function{
      if(i==null)
          {//do something
          }
      else{//do something
          }
})
}
我想根据 find 函数

的返回值执行操作,并且只有在 find 函数结束时才应检查条件。

查找函数将始终返回 null。

$.each(array,function(i,elem){              
    if(elem.id==param){
        return i;   //this is each iteration callback scope not find function scope
    }
})

find 函数应如下所示:

function find(param){
    var matched=null;            
    $.each(array,function(i,elem){              
        if(elem.id==param){
            matched=i;
        }
    })
    return matched;
}

希望这对您有所帮助。