首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在javascript中使用回调后解释输出

在javascript中使用回调后解释输出
EN

Stack Overflow用户
提问于 2015-10-06 09:43:06
回答 3查看 123关注 0票数 4

我对javascript很陌生,我正在努力理解回调。我无法理解为什么在10点之前打印20,我的理解是- func1(parameter,func2())这样的回调函数,func2()是回调函数,它在func1执行时执行,并将“参数”传递给func1。我的理解正确吗?

代码语言:javascript
复制
function timePass(length){

    console.log("finished after doing timePass for "+length +" seconds")
}

timePass(10,timePass(20));

产出如下:

在执行timePass 20秒后完成 在执行timePass 10秒后完成

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-10-06 09:54:58

这是因为执行函数timePass,然后将结果作为参数2添加。

解释正在发生的事情:

首先定义新函数"timePass",即在控制台上打印函数。

第二步执行timePass(10, /*But here you execute it again*/ timePass(20))

函数timePass(20)将首先执行,因为您添加了()

() == execute。如果您只是传递函数的名称,它将作为函数传递。当您使用()时,它将被执行,然后结果将作为参数传递。

使用回调的示例

代码语言:javascript
复制
function timePass(length, callbackFunction){

    console.log("finished after doing timePass for "+length +" seconds");

    // check if the function caller is included callback parameter
    // and check if it is function - to prevent errors.
    if (callbackFunction && typeof callbackFunction == "function") {
        // Execute the callback (timePass)
        callbackFunction(20);
    }
}

// Here you say, Execute timePass with arg 10, and then call timePass
timePass(10, timePass);
// and now callbackFunction defined above will be == timePass

// You can do also
timePass(10, anotherFunction)
// So another function will be executed after console.log()

用例

在我们使用异步代码时,经常使用回调。

例如:小提琴手

代码语言:javascript
复制
// Imagine we have function which will request the server for some data.
function getData(index) {
  // The request - response will took some time
  // 0.1s ? 15s ? We don't know how big is the data we downloading.
  var data;
  // Imagine this is an AJAX call, not timeout.
  setTimeout(function() {
    // after 30ms we recieved 'server data'
    data = 'server data';
  },30)

  return data;
}

var users = getData('users');

console.log(users); // undefined - because we returned "data" before the AJAX is completed.

/*
So we can change the function and adding an callback.
*/

function getAsyncData(index, callback) {
  var data;
  // Imagine this is an AJAX call, not timeout.
  setTimeout(function() {
    // after 30ms we recieved 'server data'
    data = 'server data';
    callback(data);
  },30)

}

getAsyncData('users', function(data) {
  console.log(data); // 'server data'
});

// OR

function processData(data) {
  console.log(data);
}

getAsyncData('users', processData); // processData also logs 'server data'
票数 2
EN

Stack Overflow用户

发布于 2015-10-06 09:53:49

实际上,您并不是在创建回调函数,而是实际上在最后一行代码中的所有内容之前调用timePass(20)。

要传递回调函数,您应该执行如下操作:

代码语言:javascript
复制
function timePass(length,callback){
    console.log("finished after doing timePass for "+length +" seconds")
    if(typeof(callback) == "function")
        callback(20);
}

timePass(10,timePass);
票数 4
EN

Stack Overflow用户

发布于 2015-10-06 09:47:26

基本上,当解释器查看这个函数时,它将调用timepass(20)来计算结果(因为您没有返回返回的内容),然后它尝试传递到外部函数。

代码语言:javascript
复制
doFunction( doSomethingElse() );

如果doSomethingElse返回1,则必须在将该1传递到doFunction之前对其进行计算。

从根本上说,您没有实际传递回调,而是调用了函数。也许你的意思是:

代码语言:javascript
复制
callback = function() { somecode; }
target = function(data, callback) { console.log('hi'); callback(); }

target(10, callback);

注意缺少(),即callback而不是callback()

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32966722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档