kaotix
kaotix

Reputation: 43

jQuery countdown to jquery event

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

Answers (4)

Jhonny D. Cano -Leftware-
Jhonny D. Cano -Leftware-

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

Raphael M&#252;ller
Raphael M&#252;ller

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

kgiannakakis
kgiannakakis

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

user57508
user57508

Reputation:

maybe the countdown plugin will help...
there's an onExpiry-event and the until-property is very helpful.

another problem you might stumble upon is: js does not support real multithreading...

Upvotes: 0

Related Questions