follow me

Redirecting Page using Jquery Timer

First and foremost, you might wondering whats the use of having these when we can use the predefine Response.Redirect([url]) method. Well, the main purpose of having this in Jquery is to allows you to set timer that fire an event after a certain amount of time. Asp.net equivalent has 'Application_BeginRequest' event where you can record the url path of the page and a Meta refesh TAG where you can set a time. But i feel more comfortable using this jquery timer, since i can also use this when im working on MVC. I will be creating a very simple example of how i do Jquery timer in redirecting page.

To begin with we need to have the the latest jQuery [compressed, 19kB] and Jquery timer inorder to work on timer.

We will be having two pages a Home.aspx and Info.aspx. The most common way in redirecting page with timer happens when you need to notify user and redirect her/him to a particular page. Or when an error occur while doing something you need to redirect the user.

Import both JS in the <head> of the Info.aspx.







Info.aspx page will hold the notification for the user. After 3 seconds it will be redirected to Home.aspx.

Here's the scripts how to use timer in redirecting a page.





On the form of Info.aspx, we just have the notification.




Successfully created a new account.


You will be redirected to Home page after 3 seconds






87

4 comments:

Anonymous said...

I have try, but not work. . why???

chenz87 said...

It would work...it has an error? tell me details.
thanks

Anonymous said...

setTimeout('document.location="'http://google.com'"', 10000);

Mahdi Saffari said...

This worked for me:
var timeout = 4;
$(document).everyTime(1000, function (i) { $('div.message p span').text(Math.abs(timeout - i)); }, timeout);
$(document).oneTime(timeout * 1000, function () { window.location = "#"; }, 1);

Post a Comment