Timer

-4

I'm developing a web site project and I'm having trouble creating a "timer".

I need it to work as follows:

  • The user reloads the credit (EX: 4 reais = 2 hours);

  • The available times are: 1,2,3 hours;

  • After reloading, the user goes on another page and presses a button that makes the timer start, counting the seconds subtracting.

There are some ready counters like Countdown Timer, but it only works by putting a future date (EX: Nov 5, 2017 15:37:25) which does not answer me, since I need fixed values.

    
asked by anonymous 30.10.2017 / 17:46

2 answers

1

I believe EasyTimer.js will answer you. It is simply a stopwatch that can work decreasing or increasing. It also works with event triggering.

The license is MIT, which is favorable. And it has several examples on the page itself. It is not difficult to use.

Example for counting down:

JavaScript:

var timer = new Timer();

timer.start({countdown: true, startValues: {seconds: 30}});
$('#countdownExample .values').html(timer.getTimeValues().toString());

timer.addEventListener('secondsUpdated', function (e) {
    $('#countdownExample .values').html(timer.getTimeValues().toString()); 
});

timer.addEventListener('targetAchieved', function (e) {
    $('#countdownExample .values').html('FIM!'); 
});

HTML:

<div id="countdownExample">
    <div class="values"></div>
</div>

See more details on the EasyTimer.js page on GitHub.There are more examples of use.

    
30.10.2017 / 18:32
1

I think your question is a bit vague because it does not make it clear how you want to control it, whether it's just on the client side or if you're saving at some bank.

But you can save the start date on the bank, as well as the time it bought in credits. From my experience, I find this solution inelegant, I'd rather you do all the calculations before and save in the database, for example a session ID or user ID, and the release time limit.

Exemplifying :

Button click time: 10/30/2017 14:58:00

Your user bought 4 reais = 2 hours.

You will save in the database that the user in question is allowed to access the system:

Until 10/30/2017 4:58 PM.

At the time of rendering anything you just check whether the current date of the request is

30.10.2017 / 18:00