SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    530
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Script to terminate remote content access after X seconds?

    I am not sure if this is the right forum, as a javascript may not be the solution, though it is a javascript that is the problem. If I am in the wrong please, apologies in advance.

    The issue is a javascript that fetches remote content (specifically, a javascript used as an Amazon affiliate to feature their 'omakase' ads on a site) and which frequently ends up slowing down my whole site because it takes too long to fetch.

    Is it possible to wrap such a javascript in another script (javascript, or php, or anything, really) that cuts out the attempt to load the script when it takes longer than X seconds? If so, I would appreciate some examples, as I am not much of a scripter. I can install and modify basic things, but that's about it.

  2. #2
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,108
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    Hi,

    I haven't had to do such a thing myself but you should be able to call a function, and use setTimeout on another cancelling function.

    Code:
    // Make Request
    
    // Give it a second to respond
    var timer = setTimeout('cancel()', 1000);
    
    function cancel() {
      // If haven't got response - check xhr's status etc.. cancel.
    }

  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    530
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would that go inside the existing script or around it? The issue is partly that I am not sure I am allowed to modify the existing script, so ideally it would need to be some sort of wrapper or something ...

    This is sort of what it looks like now (in reality, there are a bunch more parameters after the amazon_ad_tag):

    Code:
    '<script type="text/javascript"><!--
      amazon_ad_tag = "XXX";//--></script>
    <script type="text/javascript" src="http://www.assoc-amazon.com/s/ads.js"></script>'

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    530
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I finally got a response from Amazon about the matter, and they said that they are fine with the call to the script being modified.

    So, if it is doable, I would be really grateful for an example of how to apply it to this code.

    Code:
    <script type="text/javascript"><!--
      amazon_ad_tag = "XXX";  amazon_ad_width = "120";  amazon_ad_height = "240";  amazon_color_border = "1A929C";  amazon_color_background = "000000";  amazon_color_text = "1A929C";  amazon_color_link = "F96B06";  amazon_color_price = "F96B06";  amazon_color_logo = "FFFFFF";  amazon_ad_include = "fantasy;science+fiction;a+song+of+ice+and+fire;george+r+r+martin";  amazon_ad_categories = "abcd";//--></script>
    <script type="text/javascript" src="http://www.assoc-amazon.com/s/ads.js"></script>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •