1

I want to get third party function execution time in own script. Because i want to break execution if this function take more then 2 sec.Please help if any one have any idea or suggestion to achieve the same. I am using this code :--

<script>//Tag added by editor
    var myextScript = document.createElement('script');
    myextScript.type = 'text/javascript';  
    myextScript.src = '//securepubads.g.doubleclick.net/tag/js/gpt.js'; document.getElementsByTagName('head')[0].appendChild(myextScript);
    window.googletag = window.googletag || {cmd: []}; 
    window.setTimeout(function () { 
        if ( 'pubads' in window) { //check third party function.  
            googletag.cmd.push(function() {
            googletag.defineSlot('ads-name-here', 
    [[970, 90], 'fluid', [728, 90],[970, 250]], 'div-gpt-ad-1569239177281-0').addService(googletag.pubads());
            googletag.pubads().enableSingleRequest();
            googletag.pubads().collapseEmptyDivs();
            googletag.enableServices();
            googletag.cmd.push(function() { googletag.display('slot-id'); });
           $('#responce').html('`enter code here`<h3>Adds Loads Successfully !');
           }); 
        } else { 
            $('#responce').html('<h3>Adds not Loaded. Because js file not loaded with in time !');
         }
     }, 2000);
</script>
TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • Actuality i want to break my google ads code on behalf of response time. it means if google ads code take more then 2 sec then stop code execution.So you have any idea about this.How to break this execution. – Thath Singh Sep 27 '19 at 08:07
  • I tried to check js file load in DOM but nothing happen.Its take only ms to add in DOM.Not able to get fully script load time.Hope you understand my issue. – Thath Singh Sep 27 '19 at 08:09
  • @TheMaster I tried this one but get same time in both the cases. – Thath Singh Sep 27 '19 at 08:12
  • @TheMaster code something like this. myextScript.src = 'securepubads.g.doubleclick.net/tag/js/gpt.js'; document.getElementsByTagName('head')[0].appendChild(myextScript); window.googletag = window.googletag || {cmd: []}; window.setTimeout(function () { if ( 'pubads' in window) { Here my add google ads code. $('#responce').html('

    Adds Loads Successfully !'); } else { $('#responce').html('

    Adds not Loaded. Because js file not loaded with in time !'); } }, 2000);

    – Thath Singh Sep 27 '19 at 08:20
  • Are you using google apps script at all? If not remove that tag. 1. `if ( 'pubads' in window) { //check third party function` Doesn't this work? Explain what doesn't work? – TheMaster Sep 27 '19 at 15:59
  • If you have any suggestion regarding this pls share with me.Thanks. – Thath Singh Sep 30 '19 at 05:53
  • Still not able to understand. Why doesn't this work? `if ( 'pubads' in window) { //check third party functi` You're calling it after 2 seconds using settimeout.. right? – TheMaster Sep 30 '19 at 05:59
  • My ads code work fine without any check for time.If i removed this check then my code working but i want stop google script if takes more time.And this `if ( 'pubads' in window) { //check third party functi` this is not working its always return false. – Thath Singh Sep 30 '19 at 06:28
  • Check the load event. Something like `var loaded=false; myextScript.onload = ()=>{loaded=true}; settimeout(()=>{if(!loaded){document.getElementsByTagName('head')[0].removeChild(myextScript}},2000)` – TheMaster Sep 30 '19 at 06:34

0 Answers0