SoFunction
Updated on 2025-04-11

JavaScript automatically jumps to a page after x seconds

Today, I learned a new technology when I was watching videos. I usually click "Submit" or "Confirm" on a page and will automatically jump to a page.
I searched online and found that there are many ways to deal with this technology. I only wrote down three of the ones I learned in the video:
1. Use one ("target page.jsp\.htm"); to achieve direct jump;
2. Sometimes we need some prompts, such as "It will automatically jump after x seconds. If there is no jump, please click here". You can call the Delay Go To URL in Snippets in myeclipse. The following code will be automatically generated:
Copy the codeThe code is as follows:

<script language="JavaScript1.2" type="text/javascript">
<!--
// Place this in the 'head' section of your page.
function delayURL(url, time) {
setTimeout("='" + url + "'", time);
}
//-->
</script>
<!-- Place this in the 'body' section -->
<a href="javascript:" onClick="delayURL('','2000')">My Delayed Link</a>

Modify this code to:
Copy the codeThe code is as follows:

<script language="JavaScript1.2" type="text/javascript">
function delayURL(url, time) {
setTimeout("='" + url + "'", time);
}
</script>
<span style="background: red">3</span>
Automatically jump after seconds. If it does not jump, please click the link below
<a href="Target Page.jsp">Target Page</a>
<script type="text/javascript">
delayURL("", 3000);
</script>

Then it will jump directly to the "Target Page" after 3 seconds. This method is to set a few seconds to jump, and the page will not change during this process. For example, set a 3 second, and then change 3 to 2 and then 1 to jump. Please see the third method below.
3. Modify the code in method 2 to:
Copy the codeThe code is as follows:

<script language="JavaScript1.2" type="text/javascript">
function delayURL(url) {
var delay=("time").innerHTML;
//The last innerHTML cannot be lost, otherwise delay is an object
if(delay>0){
delay--;
("time").innerHTML=delay;
}else{
=url;
}
setTimeout("delayURL('" + url + "')", 1000);
// Here 1000 milliseconds are jumps every second
}
</script>
<span style="background: red">3</span>
Automatically jump after seconds. If it does not jump, please click the link below
<a href="Target Page.jsp">Topic List</a>
<script type="text/javascript">
delayURL("/news/knowledge/");
</script>

The effect of this method is to jump to this page after clicking submit on the previous page and then jump to the target page after 3 seconds (this 3 will decrease to 0).