0

我在使用以下代码段时遇到问题。我的 init 方法需要运行并完成 getLocation() 函数,然后 initializeMapp() 和 geoListen() 才能运行。我将 rsvp.js 链接为资源,但不太确定如何实现。我还尝试了 jQuery $when.done 方法。任何帮助表示赞赏。

jQuery方法:

//initial page load method
function init() {

    //get user location then builds map and listens to database
    $.when(getLocation()).done.(function () {

       //build map now that you have user location
        initializeMap();

       //Starts listening to database changes
       geoListen();

    })

}

回复方式:

 //initial page load method
 function init() {

      //get user location then builds map and listens to database
     getLocation().then(function () {

     //build map now that you have user location
     initializeMap();

     //Starts listening to database changes
     geoListen();

  })

 }   
4

1 回答 1

0

你可以让你的函数接受回调并在完成getLocation时执行它们getLocation

getLocation = function(cb1, cb2) {
  // when done 
  cb1()
  cb2()
}

function init() {
  getLocation(initializeMap, geoListen)
}

或者您可以传递一组回调引用并循环访问它们getLocation并调用它们

或者,您可以使用 Q.js 并从getLocation

于 2014-09-12T21:04:50.967 回答