Current location: Hot Scripts Forums » General Web Coding » JavaScript » Redirect Script.


Redirect Script.

Reply
  #1 (permalink)  
Old 02-09-05, 10:41 AM
RossC0 RossC0 is offline
Newbie Coder
 
Join Date: Jun 2004
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Redirect Script.

Hi,

I googled but no joy! I'm looking for a redirect script that redirects a page after 5 seconds.

Also on the page I want it to update how many seconds are left before redirection.

So far I've got:

Code:
// Redirect constructor:
function Redirect( url ) {
  if ( url ) {
	var secs = 5;
	for (i = 0; i <= secs; i++)
	{
		var secondsLeft = secs - i;
		setTimeout("timeLeft("+secondsLeft+")",1000);
	}
	window.location.replace(url);
  }
} 

// Change The Time Left Message.
function timeLeft(secondsLeft) {
	document.getElementById('seconds').innerHTML = secondsLeft;
}
Which does the redirect fine - but it doesn't update the seconds as it loops and seems to be faster than 5 seconds!

If I chuck an alert in it does update the seconds as it goes along...

Any ideas ?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 02-09-05, 12:29 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
You are starting multiple "threads" with the setTimeout since you call it 5 times in a row using the loop. All those timeouts will be running simultaneously, which means that they'll all try to change the text in the box at the same time, not a good thing.

Code:
<head>
<script>
function Redirect(secondsLeft,url) {
	document.getElementById('seconds').innerHTML = secondsLeft;
	if(secondsLeft>0){
		setTimeout("Redirect("+secondsLeft+","+url+")",1000);
	}
	else{
		window.location.replace(url);
	}
}
</script>
</head>
<body onload="Redirect(5,'http://www.google.com')">
Try this instead. It won't call the setTimeout again before the last one has ended.
You could also modify the script a bit and call myTimer=setInterval("Redirect(5,'www.google.com')" ,1000) instead (just once though!) and use clearInterval(myTimer) when it has been run five times. But that is not needed here.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 02-10-05, 06:02 AM
RossC0 RossC0 is offline
Newbie Coder
 
Join Date: Jun 2004
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up [RESOLVED] Redirect Script

Thanks!

I thought it was doing each process at the same time!

Excellent stuff one slight modification and I was away!

Final code for all those who want it!
Code:
// Redirect constructor:
function Redirect(secondsLeft,url) {
	document.getElementById('seconds').innerHTML = secondsLeft;
	secondsLeft = secondsLeft - 1;
	if(secondsLeft>0){
		setTimeout("Redirect("+secondsLeft+",'"+url+"')",1000);
	}
	else{
		window.location.replace(url);
	}
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
2 profitable script sites for sale cms-master.com General Advertisements 3 07-03-07 11:17 AM
Login and Redirect user script alistairgd Script Requests 4 01-03-05 04:30 PM
Pre-load Image Script then Redirect? thefoxbox PHP 3 11-25-04 03:29 PM
Is there any integrity of script rankings? webmaster@atmanager.com Hot Scripts Forum Questions, Suggestions and Feedback 17 08-06-04 01:12 AM
Using crond to run a php script with redirect and refresh php_newbie PHP 2 02-09-04 01:03 PM


All times are GMT -5. The time now is 10:19 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.