Search code examples
javascriptfacebooksettimeoutfbjs

Anyway to append this script to the body after setTimeout


I am trying to append this script below to the end of the body after setTimeout of 3 seconds. The issue i am having is that, I am loading the facebook javascript all.js 3 seconds after the page has loaded for performance reasons, but it does not recognize the settings, so i want to append this script to the body when i load the facebook javascript. How can i do this?

<script>
  FB.init({
    appId  : 'myappid',
    status : true,
    cookie : true,
    xfbml  : true 
  });
</script>

Solution

  • Why would you append the script? Simply invoke this method in your callback for the setTimeout:

    function initializeFacebookThing()
    {
        setTimeout(function()
        {
           if(someComponentIsLoaded)
           {
               FB.init({
                  appId: 'myappid',
                  status: true,
                  cookie: true,
                  xfbml: true
               });
           }
           else
           {
                initializeFacebookThing();
           }
        }, 3000);
    }
    

    This way, you even get loading wait time tolerance.