Reputation: 43
I am using jQuery to refresh a DIV content box every 5 seconds or so, the time is actually controllable by the end user. I want to have a countdown timer on the page which will count down until the next page refresh (or query) based on the value that is set.
For now, it can be a static value of 5 seconds but I really can't work out how to get a countdown running along side my query.
Any help is greatly appreciated.
Upvotes: 0
Views: 1318
Reputation: 18013
Well... I did something similar a while ago. I used a plugin for jQuery called jquery.timers:
this is my code:
function timerTick() {
counter--;
$("#lblRepeat").html('searching again in ' + counter + ':');
if (counter <= 0) {
counter = 60;
search();
}
}
function chkActivate_click(ev) {
var vr, t;
t = $(this);
vr = t.is(':checked');
if (vr) {
t.everyTime(1000, 'busqueda', timerTick);
} else {
t.stopTime('busqueda');
}
}
Upvotes: 0
Reputation: 971
Edit: Not sure if we understood you: Do you want a counter beeing displayed on the page?
If not:
You can set a variable in your JavaScript, e.g.
var TimeOutValue = 5000;
function OnTimeOut()
{
// Add here your code for refreshing the DIV content
window.setTimeout(OnTimeOut, TimeOutValue);
}
In the Handler for textchanged event of the input field: e.g.
function InputTextChanged()
{
TimeOutValue = document.getElementById("Id_of_Input_field").value;
}
Upvotes: 0
Reputation: 104178
You could use setInterval.
var remaining = 5;
var myInterval = setInterval ( updateCountDown, 1000 );
function updateCountDown( )
{
$("mydiv").text(remaining );
remaining --;
if (remaining == 0) {
clearInterval(myInterval );
}
}
Upvotes: 1