Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-04-2012, 07:02 PM   PM User | #1
Gemini23
New to the CF scene

 
Join Date: Nov 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Gemini23 is an unknown quantity at this point
How to time-out javascript?

I have a website where I have a search box (supplied by Javascript) that shows source information and live inventory from my suppliers website.

All works 'okay' most of the time and I am fairly happy with it but with two exceptions.

Once a day my supplier updates their inventory and their server is down for 5-30 minutes in the early hours of the morning (UK time). The problem is that my website therefore cannot load the script and the rest of the page won't load until such time as my supplier's website is back online. We have customers world-wide so it isn't ideal as 2.00am UK time obviously isn't the case elsewhere in the world.

Also the loading of the search box can at times delay the loading of my webpages (the search box is on a number of pages).

Two questions:

1. Is there a way to get the javascript to load last (it is placed quite high up on the page - above the fold)

2. If the javascript doesn't load after a number of seconds.. say 10s - is it possible to load a message in its place - ie.. "Inventory updating please call back shortly"

Thanks for any advice...
Gemini23 is offline   Reply With Quote
Old 12-04-2012, 08:13 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Gemini23 View Post
Two questions:

1. Is there a way to get the javascript to load last (it is placed quite high up on the page - above the fold)

2. If the javascript doesn't load after a number of seconds.. say 10s - is it possible to load a message in its place - ie.. "Inventory updating please call back shortly"

Thanks for any advice...
1) Yes. Place the Javascript immediately before the </body> tag.

2) Yes. Provided you can identify the name of a global Javascript variable in the downloaded script,


Code:
<script type = "text/javascript">

function testifloaded() {
if (typeof thirdpartglobalvariable == "undefined") {
alert ("Inventory updating please call back shortly");
}
}

window.setTimeout(testifloaded,10000);  // 10 seconds

</script>
Naturally the above script must come after the third-party script. Instead of the primitive alert you might prefer to display a styled message in a <span>.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 12-04-2012 at 08:18 PM..
Philip M is offline   Reply With Quote
Old 12-04-2012, 08:20 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
If the inventory is "loaded" using an AJAX call, you can control the AJAX timeout and, upon that timeout, do the same thing Philip showed.

But Philip's answer is a pretty good generic one. Even if the variable is defined, if it doesn't have the value or kind of value you are expecting, you can use that same basic pattern.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-04-2012, 09:01 PM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
you can add the defer or async attribute to the script tag so that your page does not wait on the script to load before rendering the html.

visually, you want to use HTML and CSS to indicate to all users every time that the script is "loading". after ten seconds, you tell them that it is "unavailable", and when the script arrives, it replaces the "loading"/"unavailable" text with the data/functionality provided by the script, intuitively replacing the message with functionality.

a <progress></progress> usually looks good in those kind of empty containers...
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:14 AM.


Advertisement
Log in to turn off these ads.