回调函数似乎未执行

callback function does not appear to be executing

本文关键字:执行 函数 回调      更新时间:2023-09-26

我有以下代码从github获取一个JSON对象,我正在字符串中向数组添加某些部分。

function getTree(hash) {
    var pathToTree, returnedJSON;
    pathToTree = 'https://api.github.com/repos/myaccount/myrepo/git/trees/' + hash;
    $.ajax({
        accepts: 'application/vnd.github-blob.raw',
        dataType: 'jsonp',
        url: pathToTree,
        success: function (json) {
            returnedJSON = json;
        },
        error: function (error) {
            console.debug(error);
        }
    });
    return returnedJSON;
}
function parseTree(hash) {
    var objectedJSON, objectList = [], i, entry;
    objectedJSON = getTree(hash, function () {
        console.debug(objectedJSON);                    // this is not appearing in console
        for (i = 0;  i < objectedJSON.data.tree.length; i += 1) {
            entry = objectedJSON.data.tree[i];
            console.debug(entry);
            if (entry.type === 'blob') {
                if (entry.type.slice(-4) === '.svg') {     // we only want the svg images not the ignore file and README etc
                    objectList.append(i.content);
                }
            } else if (entry.type === 'tree') {
                    objectList.append(parseTree(getTree(entry.sha)));
            }
        }
    });
    return objectList;
}
$(document).ready(function () {
    var objects = parseTree('master', function () {
        console.debug(objects);
    });
});

我有检索JSON对象的代码,但在试图解析它时遇到了麻烦(也就是提取我想要的比特)。我使用的回调似乎不起作用,我想知道是否有人能帮我查一下。

具体来说,我可以为我选择的任何函数添加回调吗?我必须对那个函数做些什么吗?

我已经修复了代码,以说明如何进行。

function getTree(hash, cb) {
    // notice that I copy the callback and hash references to have access to them in this 
    // function's closure and any subsequent closures, like the success and error
    // callbacks.
    var pathToTree, returnedJSON, cb = cb, hash = hash;
    pathToTree = 'https://api.github.com/repos/myaccount/myrepo/git/trees/' + hash;
    $.ajax({
        accepts: 'application/vnd.github-blob.raw',
        dataType: 'jsonp',
        url: pathToTree,
        success: function (json) {
            returnedJSON = json;
            // if anything was passed, call it.
            if (cb) cb(json);
        },
        error: function (error) {
            console.debug(error);
            // an error happened, check it out.
            throw error;
        }
    });
    return returnedJSON;
}
function parseTree(hash) {
    var objectedJSON, objectList = [], i, entry;
    objectedJSON = getTree(hash, function (objectedJSON) {
        console.debug(objectedJSON);                    // this is not appearing in console
        for (i = 0;  i < objectedJSON.data.tree.length; i += 1) {
            entry = objectedJSON.data.tree[i];
            console.debug(entry);
            if (entry.type === 'blob') {
                if (entry.type.slice(-4) === '.svg') {     // we only want the svg images not the ignore file and README etc
                    objectList.append(i.content);
                }
            } else if (entry.type === 'tree') {
                    objectList.append(parseTree(getTree(entry.sha)));
            }
        }
    });
    return objectList;
}
$(document).ready(function () {
    var objects = parseTree('master', function () {
        console.debug(objects);
    });
});

就我所见,您没有向函数传递回调:

function getTree(hash) {

你使用的是:

objectedJSON = getTree(hash, function () {

类似地,此函数没有回调参数:

function parseTree(hash) {

你使用的是:

var objects = parseTree('master', function () {

修改你的功能如下:

function getTree(hash, fn) {  ...  }
function parseTree(hash, fn) {  ...  }

然后在需要时使用fn()调用fn

getTree函数添加第二个参数。类似的东西

function getTree(hash, callback)

在Ajax选项中使用"jsopCallback"参数

$.ajax({
      ...
      jsopCallback: callback,
      ...