简体   繁体   English

同步Javascript函数调用

[英]synchronizing Javascript function calls

I am trying to include Dailymotion in my web app. 我试图将Dailymotion包含在我的Web应用程序中。 I have one javascript file (for Dailymotion) and an inline javascript.I am trying to get the recomemmended videos from DM, for which I have a function ready. 我有一个javascript文件(用于Dailymotion)和一个内联javascript。我正在尝试从DM获得推荐的视频,为此我已经准备好功能。

Dailymotion.js looks something like this Dailymotion.js看起来像这样

login();
function login(){
  getScreenname();
}
function getScreename(){
  // print some name
}
function getRecoVideos(callback){
  callback(result);
}

Inline.js Inline.js

getRecoVideos(function(result){
  console.log(result);
});

In the inline.js file, the call to getRecoVideos doesn't work because, the login is not finished. inline.js文件中,对getRecoVideos的调用不起作用,因为登录尚未完成。 How can I synchronize the calls? 如何同步通话?

Per this documentation DM takes a response function that you can use to then call the getRecoVideos() function. 根据该文档, DM使用了一个响应函数,您可以使用该函数然后调用getRecoVideos()函数。

UPDATE: 更新:

To make it so you can receive the event from anywhere you could do the following: 为此,您可以从任何地方接收事件,您可以执行以下操作:

DM.login(function() {
    $(document).trigger("dm-logged-in");
});

And then to receive the event: 然后接收事件:

$(document).on("dm-logged-in", function() {
    // Do what you want here...
});

This is using jQuery. 这是使用jQuery。 If you don't use jQuery see this for more data on how to dispatch events in Java Script: How to trigger event in JavaScript? 如果您不使用jQuery,请参见此内容以获取有关如何在Java Script中调度事件的更多数据: 如何在JavaScript中触发事件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM