PDA

View Full Version : javascript timeout



mutago
04-29-2014, 08:53 PM
I have a form that redirects to a site if its online or print error message if its online.

Now I want to delay and redirect to the site whether offline or online after 5 seconds elapse.

I have added setTimeout() but it does not delay for 5 minutes before redirecting to another site.

Any help




<script type="text/javascript">
//start checking onlineconnection status
function checkconnection() {

var status = navigator.onLine;
if (status) {

window.onload=timeout;
function timeout(){
window.setTimeout("redirect()",5000)}



window.location="www.online.com"



} else {

window.location="www.offline.com"
}
}
</script>



on the body, i have onload() event


<body onload="checkconnection();">



Any Help

fastsol1
04-29-2014, 10:59 PM
This is not a php issue, you're asking about javascript. You need to ask in the proper forum.

traq
04-30-2014, 12:31 AM
Thread moved.


I have a form that redirects to a site if its online or print error message if its online.
Your example code does not seem to attempt this: looks like it intends to redirects to one site if online, and to a different site if offline.


Now I want to delay and redirect to the site whether offline or online after 5 seconds elapse.
I have added setTimeout() but it does not delay for 5 minutes before redirecting to another site.
What does it do?

One thing I notice right away is that you specify a callback in setTimeout named redirect, but your code defines no such function.

Also note that onLine is not the most reliable of properties. It works differently, and means different things, in different browsers. You might want to read this article (http://www.html5rocks.com/en/mobile/workingoffthegrid/) for more ideas about working online/offline (for example, checking via AJAX or listening to the AppCache error event).

jscheuer1
04-30-2014, 12:43 AM
Your post is very confusing:


redirects to a site if its online or print error message if its online.

It can't do both. And:


Now I want to delay and redirect to the site whether offline or online after 5 seconds elapse.

I have added setTimeout() but it does not delay for 5 minutes before redirecting to another site.

"the site" - which site(s) do you mean, and is it minutes or seconds you want?

Please restate your question in a clearer fashion.

mutago
04-30-2014, 11:08 PM
Thank you. i want to check if internet connection is available and then delay for about 5 seconds before redirecting to onlinesite.com and if internet connection is not available then delay for about 5 seconds before redirecting to eg. offlinesite.com

thank you

traq
04-30-2014, 11:50 PM
The confusing part is why you would try to redirect (anywhere) if the user was offline.

mutago
05-01-2014, 02:41 AM
am not redirecting anywhere. okay just consider the offlinesite.com to be offline.html and onlinesite.com to be online.html

thanks