0

所以假设我正在调用这样的函数:

some_function('pages',{attr1: 1, attr2: 2},function(){
    alert('the function is ready!');
}

现在如何设置“some_function()”函数以便返回给调用者它已准备好并使警报消失?

谢谢 :)

4

3 回答 3

1

你的意思是这样的吗?

function some_function(type, options, callback) {
  if (some_condition) {
    callback();
  }
}
于 2012-06-13T15:53:36.277 回答
1

假设签名some_function看起来像这样:

function some_function(name, data, callback)

你只需要callback在你准备好时打电话。

function some_function(name, data, callback){
    // do whatever
    if(typeof callback === 'function'){
        callback(); // call when ready
    }
}
于 2012-06-13T15:54:13.363 回答
1

我认为你的意思是回调。也许是这样的:

function some_function(param1, param2, callback) {

    // normal code here...

    if ( typeof callback === 'function' ) { // make sure it is a function or it will throw an error
        callback();
    }
}

用法:

some_function("hi", "hello", function () {
    alert("Done!");
}); 
/* This will do whatever your function needs to do and then,
when it is finished, alert "Done!" */

注意:把 your 放在return子句之后if

于 2012-06-13T16:02:00.207 回答