473,386 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

include js timeout - help needed :)

Hi there, I'm a noob to javascript, so was hopping to find some help here...

I believe that what I need is simple, still haven't found any solution.

I have a website serving ads, and links from several systems, and they always come in a format like:

<script language=javascript src="http://something.com/script.php?id=26758765"></script

This is fine, however I would like to establish a time-out for this, like for instance, if something.com/script.php?id=26758765 is not available in 2 seconds, the page would still load instead of trying to load that javascript forever.

What can I do?
Please post some example code. I'm a noob... sorry.
Feb 26 '07 #1
12 2362
有点明白,但是英文不好,不知道怎么帮你!
不知道你是不是用document.write()或者document.writeln() . 这两者必须在页面加载前用,要不然你的页面将会被打印出来的值覆盖!
Feb 26 '07 #2
acoder
16,027 Expert Mod 8TB
Hi there, I'm a noob to javascript, so was hopping to find some help here...

I believe that what I need is simple, still haven't found any solution.

I have a website serving ads, and links from several systems, and they always come in a format like:

<script language=javascript src="http://something.com/script.php?id=26758765"></script

This is fine, however I would like to establish a time-out for this, like for instance, if something.com/script.php?id=26758765 is not available in 2 seconds, the page would still load instead of trying to load that javascript forever.

What can I do?
Please post some example code. I'm a noob... sorry.
Welcome to The Scripts.

Is the PHP generating javascript code.

If it's static, why not include the javascript in your PHP page?
Feb 27 '07 #3
acoder
16,027 Expert Mod 8TB
有点明白,但是英文不好,不知道怎么帮你!
不知道你是不是用document.write()或者document.writeln() . 这两者必须在页面加载前用,要不然你的页面将会被打印出来的值覆盖!
Translation please!!
Feb 27 '07 #4
Thanks for your reply.
That little piece of code is the same but the content it generates is random, just like adsense, or other networks.

My problem is, that if you put that piece of code in the middle of a page it will work fine, however if the ad server is down when I try to load my page there is a problem...
My browser starts to read the page, but will hangs in the middle of that page trying to read that javascript forever.
If there is no connectivity to that ad server, it will take forever to give up, and load the rest of content in my page.

If I put that include on top of my pages, it first tries to load that javascript... so it will not open my webpage before that includes. If the ad server brings a 404, the page loads fine, but if te server is down, no error message will be given, so my website and my browser will try to open that includes forever, before everything else.

What I want is to limit that wait for that includes.

Something like:

start measuring time,
if loading time > 2 seconds = die(output text : ADS not available.)
else if
loading time < 2 seconds
include file.js

Is it possible to check for a 200 OK response within 2 seconds in javascript?

thanks
Feb 27 '07 #5
Ha!, now I find out the solution :)

I have been reading some javascript books, and I after all I get this to work.

replace your ad with this example:

Go to the exact place you want your ads displayed, like on the sidebar, or the middle of the page and replace:

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript" src="http://ads-or-whatever-it-is.com/script.php?id=24324242"></script>
with:

Expand|Select|Wrap|Line Numbers
  1. <div id="name_of_your_add"><p>Loading Partners or whatever you like here...</p></div>
then on the very bottom end of the page, add:
(should be your footer if you're using a script portal, blog or whatever).

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript"><!--
  2. var old_document_write = document.write;
  3. var name_of_your_add_data = "";
  4. document.write = function(s) {
  5. name_of_your_add_data += s;
  6. }
  7. // --></script>
  8. <script language="javascript" type="text/javascript" src="http://ads-or-whatever-it-is.com/script.php?id=24324242"></script>
  9. <script language="javascript"><!--
  10. document.write = old_document_write;
  11. document.getElementById("name_of_your_add").innerHTML = name_of_your_add_data;
  12. // --></script>
and you're done.

This will allow all page to load fine, and in the end, if network is not available, or ads are not available because they are slow, they will not interfere with your page load.

Check my website at http://hi7.co.uk
Feb 27 '07 #6
acoder
16,027 Expert Mod 8TB
A simple solution in the end. The most elegant solution would be to detect the loading of the document and timeout after 2 seconds, but if it's not possible, then why not? I am unsure why your code replaces document.write though. Is that really necessary?
Feb 27 '07 #7
A simple solution in the end. The most elegant solution would be to detect the loading of the document and timeout after 2 seconds, but if it's not possible, then why not? I am unsure why your code replaces document.write though. Is that really necessary?
And how to detect the loading of the document and timeout after 2 seconds?
Can you post an example?

I have used this, because I don't know how to do what you've suggested...

Imagine that file.js , takes 30 seconds to load due to network problems, or server load.
I have done this because i don't want to wait 30 seconds, in order to load the rest of the page.
But how to detect the loading of the document and timeout after 2 seconds?
Feb 27 '07 #8
acoder
16,027 Expert Mod 8TB
I said it's a more elegant solution, not that it's possible. See this thread. It might help. What you need effectively is an event handler that can detect chnages in the loading state.

What I suggest you can do is get the time using Date(). Then make a request to load the document and check after two seconds. If not loaded, then abort. To check if it's loaded, you could check for an object, e.g. if you have an object with id "ad_id", you could try:
Expand|Select|Wrap|Line Numbers
  1. if (document.getElementById("ad_id")) {...
I haven't tried this so I don't know if it will work.
Feb 27 '07 #9
thanks anyway.

cheers
Feb 28 '07 #10
Translation please!!
Sorry!
I'm English is poor!
Feb 28 '07 #11
acoder
16,027 Expert Mod 8TB
Sorry!
I'm English is poor!
It doesn't matter... most people can understand even if there are quite a few grammatical mistakes. In fact, people born in the UK/US/English speaking countries make so many mistakes too!

But, with Mandarin (which I assume it is), most users here have no chance of understanding one letter never mind a word of it.

btw, it should be "My English is poor", but no worries, I know how hard it is to communicate in a second (or even third) language.
Feb 28 '07 #12
acoder
16,027 Expert Mod 8TB
thanks anyway.

cheers
There's one other possibility if you're up for it. What you can do is load the page without the ads. Then use Ajax to get the ads. To do this, have a page on your website that loads the ads from the different servers, then make a request to that page. Then you don't have to worry about how long it takes because it is asynchronous.
Feb 28 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Frank Rizzo | last post by:
Hello, My c# based windows service takes a while to dispose. I have to release bunch of resources all over the place and unfortunately it can take 20-40 seconds before I can cleanly exit. ...
8
by: Joe Abou Jaoude | last post by:
hi, I have a web app with forms authentication and a timeout session of 20 mins for security reasons. I recently added a feature that allows users (if they want to) to automatically log in...
5
by: Tim W. | last post by:
Folks. In a B2B Procurement system we've created, we got following Session-Issue: Configuration: We are using IIS 6.0 and added SQL-Based-Sessions in web.config with a timeout of 240 minutes...
8
by: Shalini | last post by:
If I have an ASPX page that is just sleeping for 3 minutes ormore and later does some processing and the ExecutionTimeout parameter in the machine.config is 90 seconds then the page does not...
17
by: jensen bredal | last post by:
Hello, i'm struggling with a somehow badly understood session scenario. I provide acces to my pages based on form authentication using Session cookies. Som of my pages are supposed to be...
2
by: WB | last post by:
Hi, I have a Windows Application in C# that has some long-running codes. When I tried to run the program in debug mode, it eventually gave an error: Timeout expired. The timeout period elapsed...
4
by: UJ | last post by:
I have a page where the user can upload a video file. As you can guess, this may take a while. Is there a way I can change the session timeout for just this one page? I would also want to change...
22
by: Nick Craig-Wood | last post by:
Did anyone write a contextmanager implementing a timeout for python2.5? I'd love to be able to write something like with timeout(5.0) as exceeded: some_long_running_stuff() if exceeded:...
25
by: =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post by:
I tried: <sessionState timeout="1"> </sessionState> bounced IIS, and after 1 minute still had a session. ??? -- thanks - dave
5
by: Masso | last post by:
Hallo to the group, my question is this: where i have to set timeout in a web application? If i set into sessionState with property Timeout="60", after 20 minutes the session is over. Always...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.