【问题标题】:Check when function finishes reading all the xmls检查函数何时完成读取所有 xml
【发布时间】:2015-07-07 00:38:47
【问题描述】:

我有一个函数可以从 7 个不同的 URL 读取 XML 提要,然后添加到 JSON 对象中。我需要知道函数的执行何时完成,然后我需要将 Json 提要发送到另一个函数。

simpleAJAXLib.init();这是读取所有xml并将其放入Json对象jsonStr2的函数,我需要将此Json对象传递给另一个方法。

passtoSecMethod(jsonStr2);

我正在尝试 Promise,但仍然是同样的问题。

$(document).ready(function() {

    d = new $.Deferred();
    myfunction();

    function myfunction() {
        promise = longfunctionfirst().then(shortfunctionsecond);
    }

    function shortfunctionsecond() {

        alert("second function finished");
        return d.promise()
    }

    function longfunctionfirst() {
        var URLCollection = [
            "http://news_national_3354.xml",
            "http://news_theworld_3356.xml",
            "http://news_sport_3168.xml",
            "http://news_ent_3169.xml",
            "http:///news_lifestyle_3171.xml",
            "http://news_travel_3325.xml",
            "http://news_money_505.xml",
            "http://news_tech_506.xml"
        ];
        for (var i = 0; i < URLCollection.length; i++) {
            simpleAJAXLib.init(URLCollection[i]);

        }
        d.resolve();
        return d.promise();
    }
});

警报应该在 longfunctionfirst 完成时打印,但它会在函数启动时立即打印

【问题讨论】:

  • 你应该看看promises
  • 有人可以帮忙吗?

标签: javascript jquery html json function


【解决方案1】:

Promise 应该可以解决问题。它们并不像看起来那么难。这是一个教程:promises

【讨论】:

  • 我已经使用 Promise 更新了我的问题,但问题仍然存在,请您查看我更新的问题
  • 需要设置你需要听的一个/多个承诺的部分是simpleAJAXLib.init()。当promise 发送到simpleAJAXLib.init() 已经完全解决,然后你就做你的事。
猜你喜欢
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 2017-01-26
  • 2011-10-18
  • 2014-06-15
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多