Timer Problem

liunx

Guest
Hello everyone,
I am intending to use timer in one of my webpage of my application(ASP.NET and VB.NET). I want that if user doesn't click on SAVE button within 60 seconds, some event will be raised and he will be redirected to another page. I haven't used timer so someone please guide me how to use it.. What code do I need to write?


GauravHi,

you can't do that with server side timer.
when you're done parsing the page, the server side code 'deliver' the page to the browser and the connection between both is lost.
So when the timer fires (it does fire serverside), you can't reach the browser to redirect to another page.

You can however accomplish it with javascript, let me try to work something out.

MichelHi,

i've worked someting out.
I hope the code explains itself, of not, please reply.

<SCRIPT LANGUAGE = "JavaScript">
<!--

//delay in milliseconds
var delay = 5000;
//temp variable
var timerID = null;
//variable to store whether the button is clicked or not
var clicked = false;

//this function registers the RedirectPage to a new timer
//the timer fires after the 'delay'
function InitializeTimer()
{
timerID = self.setTimeout("RedirectPage()", delay)
}

//the function to check if the user clicked the button
function RedirectPage()
{

//did he click?
if (clicked != true)
{
//NO, redirect
alert('you will be redirected');
this.location = "anotherpage.aspx";
}
else
{
//Yes, he can stay
alert('you will NOT be redirected');
}

}
//this function will be called when the user clicks the button
function setClicked()
{
clicked = true;
}
//-->
</SCRIPT>

then bind the InitializeTimer method to the body's onLoad event:
<body onload="InitializeTimer();">

and the html code to create a button:
<input type=button onclick ="setClicked();" value = "click this and you will not be redirected!">

Hope this helps.

MichelThank you Michel. This seems to be the solution to my problem. But actually I am making online testing application. I want to do something different than this. When user doesn't click on save button within specified time, a connection will be made to the database and next question will be displayed.How? If He/She clicks on save button within time, his answer will be saved in database, next question will be retrieved from database and displayed to the user. Also, I want to store the result and count of questions in some persistent storage. Had it been in .NET language I would have used Session to store it. I am new to JavaScript. Can you tell me some code how to do all these things. Thank you again.

GauravHi,

if you want to reconnect to the database and display the new question, you have to reload the page.
You can store the 'isclicked' variable in a session.
When a user clicks the button to save the form, you can save it all to the database, and then the page will be reloaded. You can then use the session object to check if the page was reloaded via the save-button or via the javascript reload.
 
Back
Top