How to detect when facebook's FB.init is complete

后端 未结 12 1975
我寻月下人不归
我寻月下人不归 2020-11-28 01:28

The old JS SDK had a function called FB.ensureInit. The new SDK does not seem to have such function... how can I ensure that I do not make api calls until it is fully initia

12条回答
  •  有刺的猬
    2020-11-28 01:54

    The Facebook API watches for the FB._apiKey so you can watch for this before calling your own application of the API with something like:

    window.fbAsyncInit = function() {
      FB.init({
        //...your init object
      });
      function myUseOfFB(){
        //...your FB API calls
      };
      function FBreadyState(){
        if(FB._apiKey) return myUseOfFB();
        setTimeout(FBreadyState, 100); // adjust time as-desired
      };
      FBreadyState();
    }; 
    

    Not sure this makes a difference but in my case--because I wanted to be sure the UI was ready--I've wrapped the initialization with jQuery's document ready (last bit above):

      $(document).ready(FBreadyState);
    

    Note too that I'm NOT using async = true to load Facebook's all.js, which in my case seems to be helping with signing into the UI and driving features more reliably.

提交回复
热议问题