S
S
sashavol2015-09-30 21:22:59
JavaScript
sashavol, 2015-09-30 21:22:59

Change countdown timer logic?

Hello, I know js superficially and I want to change the timer logic for the sake of interest, of course I can take a new one, that's not the point.
Now its logic works from the time 0:00. But looking at the code, it seems to me that we take the current time and add 24 hours (1000 * 60 * 60 * 24) to target_time. I want to implement it this way in order to take the current time and add my own to it.
At least that way, then I'll procrastinate to remember in cookies so as not to update the timer for a person every time.
Yes, maybe a bike.


var set_slide = function(selector, slide) {
$(selector).text(slide);
}
var time = new Date();
var target_time = new Date(time.getFullYear(), time.getMonth(), time.getDate());
target_time = target_time.valueOf()+1000;
var tick = function(init) {
if (init == undefined) {
init = false;
}
var current_time = new Date();
current_time = current_time.valueOf();
if (current_time > target_time) {
var time = new Date();
target_time = new Date(time.getFullYear(), time.getMonth(), time.getDate());
target_time = target_time.valueOf()+1000*60*60*24;
}
var time_diff = Math.floor((target_time - current_time)/1000);
var second_2 = time_diff % 10; time_diff = Math.floor(time_diff/10);
var second_1 = time_diff % 6; time_diff = Math.floor(time_diff/6);
var minute_2 = time_diff % 10; time_diff = Math.floor(time_diff/10);
var minute_1 = time_diff % 6; time_diff = Math.floor(time_diff/6);
var hour_2 = Math.floor(time_diff/15);
var hour_1 = time_diff % 10;
set_slide('span.secondPlay5', second_2);
if ((second_2 == 9) || init) { set_slide('span.second6Play5', second_1);
if ((second_1 == 5) || init) {set_slide('span.minutePlay5', minute_2);
if ((minute_2 == 9) || init) { set_slide('span.minute6Play5', minute_1);
if ((minute_1 == 5) || init) {set_slide('span.hourPlay5', hour_1);
if ((hour_2 == 9) || init) {set_slide('span.hour2Play5', hour_2);}}}}}
}
tick(true);
setInterval(tick, 1000);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivanq, 2015-09-30
@sashavol

Let's do this: here's a fiddle for you, look what's wrong. The timer wrote from the beginning, I couldn’t figure out your code
jsfiddle.net/Ivanq/qnprhnLs

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question