javascript - Redirect script - Stack Overflow

admin2025-04-19  0

currently, I'm redirecting users after x seconds to another page, but what I want to do is, redirect users after y seconds and the timer will start only after user click a link on my page, currently the timer starts only after the user visit my page.

Here is the code I found doing google search -


<head>
<script>
function js_wait()
{
setTimeout (js_go(), 3000); // 3000 ms = 3 secs
}
function js_go()
{
window.location = "mynexpage.php";
}
</script>
</head>
<body>
<a href="" onclick="js_wait(); return false;">My link</a>
</body>
  • The body shows a link
  • On click, it calls the javascript function "js_wait"
  • js_wait starts a timer for x seconds, then calls js_go
  • js_go redirects to the page.

I was able to make it redirect onclick but it redirects immediately after clicking the link, but I want it to wait y seconds before redirecting.

currently, I'm redirecting users after x seconds to another page, but what I want to do is, redirect users after y seconds and the timer will start only after user click a link on my page, currently the timer starts only after the user visit my page.

Here is the code I found doing google search -


<head>
<script>
function js_wait()
{
setTimeout (js_go(), 3000); // 3000 ms = 3 secs
}
function js_go()
{
window.location = "mynexpage.php";
}
</script>
</head>
<body>
<a href="" onclick="js_wait(); return false;">My link</a>
</body>
  • The body shows a link
  • On click, it calls the javascript function "js_wait"
  • js_wait starts a timer for x seconds, then calls js_go
  • js_go redirects to the page.

I was able to make it redirect onclick but it redirects immediately after clicking the link, but I want it to wait y seconds before redirecting.

Share edited Oct 21, 2012 at 7:00 Anirudh Ramanathan 46.8k23 gold badges135 silver badges194 bronze badges asked Oct 21, 2012 at 6:57 Phillip MclaurenPhillip Mclauren 4173 silver badges17 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

Dont activate the method instead just pass the function signature like this:

<head>
<script>
function js_wait()
{
setTimeout (js_go, 3000); // 3000 ms = 3 secs
}
function js_go()
{
window.location = "mynexpage.php";
}
</script>
</head>
<body>
<a href="" onclick="js_wait(); return false;">My link</a>
</body>

Remove () from setTimeout function parameter.

setTimeout (js_go, 3000); // 3000 ms = 3 secs

If you have it there, it means you pass the result of the function call rather then the function reference.


As background information, setTimeout can be used in two different ways: either you pass it a function reference, like what you have done, or you pass a string, which will be evaluated by the time setTimeout fires. This means that if you want to pass some parameters to setTimeout, you'd either pass them as a string (like setTimeout('js_go("http://google.fi")', 3000)) or wrap them to a function, which is the preferred way (setTimeout(function() {js_go("http://google.fi")}, 3000)).

However, since you don't use parameters here, you should just pass the function reference like shown in the code block above.

Try this please much simpler: Working demo http://jsfiddle/FE4cT/

For redirect this is key: window.parent.location

Rest I hope it will fit your need :)

code

function js_wait() {
    setTimeout(function() {
        window.parent.location = "http://www.google.";
    }, 3000);

}​

Sample

 window.setTimeout(function() {
        window.location.href = 'http://www.google.';
    }, 3000);

i.e. in your specific case:

  window.setTimeout(function() {
            window.location = 'mynexpage.php';
        }, 3000);

Simply write as :

    function js_wait_redirect() {
      setTimeout(
           function(){
              window.location = "mynexpage.php";
           }
           , 3000); // 3000 ms = 3 secs
    }
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745070630a283256.html

最新回复(0)