Click to See Complete Forum and Search --> : Refreshing in Javascript...?


gilly914
October 13th, 2005, 01:04 PM
I need to redirect the page to another address, after a certain interval.
I know I can do it with meta tags, but in my specific case, I don't want to, and I can't... so I need to know how to do this in javascript...

I wrote :
<script language="Javascript">
SetInterval("RefreshPage()", 2000);

function RefreshPage() {
window.location = "http://MySite.com/";
}
</script>

Is this the right way of doing this? Or should I try something else...?

It just doesn't seem to work this way...

PeejAvery
October 13th, 2005, 02:56 PM
That won't work. It is "setTimeout()" not "setInterval()" You should clean it up though.

<script language="JavaScript">
setTimeout("window.location='http://MySite.com/'", 2000);
</script>

Dr. Script
October 13th, 2005, 04:04 PM
<script language="Javascript">
SetInterval("RefreshPage()", 2000);

function RefreshPage() {
window.location = "http://MySite.com/";
}
</script>Your code is perfectly fine to use, just you have several errors. Here it is fixed:<script type="text/javascript">

setInterval(RefreshPage, 2000);

function RefreshPage() {
window.location.href = "http://MySite.com/";
}

</script>What is bolded are changes that should be made, but aren't absolutely required to function. The udnerlined text is the cause. setInterval and setTimeout are case-sensitive, and they begin with a lowercase s, not an uppercase one.

The code provided by peejavery is, however, a better way to do it.

PeejAvery
October 13th, 2005, 04:17 PM
Remember, gilly914, that there is one difference between setInterval() and setTimeout().

setInterval(code, x) - executes and returns to execute every x milliseconds.
setTimeout(code, x) - executes after x milliseconds.

Since you are loading to a new page, this will not be an effect. But if you call one of these in the page you are in, setInterval keeps running. setTimeout runs and stops.

Just for your future use.

gilly914
October 14th, 2005, 02:53 PM
Thank You All...

You all helped me get it right...
The only problem was the letter "s" in the function setInterval needed to be lowercase...

This seemed to work fine...
window.location = "http://MySite.com/";

Thanks Again, gilly914