简体   繁体   English

多次启动自定义JS函数?

[英]Fire custom JS function more than once?

My issue is when I try to fire SocialAPI(...) several times on one page it will only fire the first event and won't complete the other requests. 我的问题是,当我尝试在一个页面上多次触发SocialAPI(...)时,它将仅触发第一个事件,而不会完成其他请求。 I don't understand how this is - I'm relatively new to learning this language and I'm a little stuck here. 我不明白这是怎么回事-我对这门语言的学习还比较陌生,我有点被困在这里。

I've tried to test each call I'm using on their own and they each work, it only seems to return one and ignore the other requests. 我已经尝试过测试自己使用的每个呼叫,并且每个呼叫都能正常工作,它似乎只返回一个而忽略其他请求。

The function accesses the API running on my server to access multiple social media APIs (not sure if this is relevant). 该函数访问在我的服务器上运行的API,以访问多个社交媒体API(不确定是否相关)。

I've checked the log and their is no error in the code. 我已经检查了日志,它们在代码中没有错误。

Thank you for any help in advance. 感谢您的任何帮助。

For example 例如

index.html (some page) index.html(某些页面)

<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>

<script>
    $(document).ready(function() {
        SocialAPI("div1", "twitter", "uid");
        SocialAPI("div2", "facebook", "uid");
        SocialAPI("div3", "instagram", "uid");
    });
</script>

SocialAPI() Function Shortened SocialAPI()函数缩短

function SocialAPI(target, platform, uid, mode) {
    if(!target) {target="";} else { target = "#" + target}
    if(!platform) {platform="";}
    ...so on for each...

    var APIBase = "https://api.domain.com/?platform=" + platform + "&uid=" + uid + "&mode=" + mode;

    if(platform == "instagram") {
    $.ajax({
      url: APIBase,
      dataType: "json",
      success: function(r) {
          $(target).html(r.data.counts.followed_by);
      }
        });
    } else if(platform == "twitter") {
    ...so on for diff platforms...
    }
  } else {
    return false;
  }
}

Try 尝试

function loadSocial(){
    SocialAPI("div1", "twitter", "uid");
    SocialAPI("div2", "facebook", "uid");
    SocialAPI("div3", "instagram", "uid");
}

Then update: 然后更新:

$(document).ready(function() {
    loadSocial();
});

Hit enter too soon, Add the following to the first line of your SocialAPI function: 点击Enter太早了,将以下内容添加到SocialAPI函数的第一行:

console.log("Platform", platform);

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

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