Subash
Subash

Reputation: 119

Timer display using Javascript/Jquery

I'm working on a thick client application which uses JavaScript and jQuery. My Session times out if it's left idle for 45 seconds or more (depending on different business/user).

Currently the timeout event is triggered by the vendor code to which I do not have access. I would like to display a timer on the screen if the session is found to be idle beyond a specific time threshold (for example: 25 seconds).

At the same time the timer should also match (marginal diff can be managed) with the third party timer (to which I do not have access).

Can someone suggest the best solution for this?

Upvotes: 1

Views: 246

Answers (3)

Subash
Subash

Reputation: 119

Halo,

I finally managed to try a timer in my application (touch based). The following code works well for me.

var count = 50;
$(document).ready(function () {
var Timer = $.timer(function() {
    $('#counter').html("Your Session will expire in " + --count + "seconds");

});
Timer.set({ time : 1000, autostart : true });
    $(this).mousemove(function (e) {
                idleTime = 0;
                count = 50;
                Timer.reset();
                Timer.stop();                   
            });
            $(this).keypress(function (e) {
                idleTime = 0;
                count = 50;
                Timer.stop();
                Timer.reset();
            });

    var idleInterval = setInterval(function (){
        idleTime = idleTime + 1;
        if (idleTime > 10) 
        { 
        Timer.set({ time : 600, autostart : true });            
        $('#counter').css("display", "block");  

Upvotes: 1

Michael Mao
Michael Mao

Reputation: 10168

Okay I don't quite get your logic, but a simple search on Google for jQuery plugin timer gives me:

http://www.tripwiremagazine.com/2013/04/jquery-countdown-scripts.html

A lot...

Hope one of them suits your needs.

Upvotes: 1

johnkavanagh
johnkavanagh

Reputation: 4674

It's worth sharing any code you already have so that we can offer more precise advise for you.

However, to give you some pointers, take a look at the JavaScript window.setTimeout() method. It essentially triggers a callback after a set period of time. It's fairly safe to say that if your vendor is using JavaScript to keep track of time, then this is the same basic method they will be using too.

You can set and reset a timeout very easily so once set, you could listen for user interaction events (scrolling, clicking, etc) and use these to trigger a reset on the timeout (putting the 'count down' back to the start).

Do a bit of research, I'm sure you'll be able to develop a suitable solution!

With regards to interfacing with your vendor's timeout, without much more information none of us will be able to help. It doesn't sound like you have any control over this so it may not be possible (perhaps contact the vendor and ask?).

If you know what the timeout limits are for each use-case, you could ensure that your timeouts match theirs. However, this would be a little bit annoying given it's code-repetition and would also mean your application would 'break' if your vendor changes their timings.

TL/DR: Read into the JavaScript window.setTimeout() method if you want to write something that will do what you describe. However, the weak link will be that you don't have access to your vendor's timeout routines. With that in mind, my first step would be to contact the vendor and ask, they may have something in their API already available that you're unaware of!

Upvotes: 1

Related Questions