I made this little function from code snippets around the net. It does what its expected to do except that it decreased by 2 seconds every countdown tick instead of one, if i refresh the page it goes back to the original time again. Can you guys see what the problem is?
jQuery.countdown = function(selector, datevalue) {
var amount = datevalue*1000;
var curdate = new Date();
curdate = curdate.getTime();
var difference = amount - curdate;
if(amount < 0 || curdate >= amount){
$(selector).html("Done");
}
else{
datevalue--;
var daysRemaining = Math.floor(difference / 1000 / 60 / 60 / 24);
var hoursRemaining = Math.floor(difference / 1000 / 60 / 60 - (24 * daysRemaining));
var minutesRemaining = Math.floor(difference / 1000 / 60 - (24 * 60 * daysRemaining) - (60 * hoursRemaining));
var secondsRemaining = Math.floor(difference / 1000 - (24 * 60 * 60 * daysRemaining) - (60 * 60 * hoursRemaining) - (60 * minutesRemaining));
$(selector).html(daysRemaining+':'+hoursRemaining+':'+minutesRemaining+':'+secondsRemaining);
setTimeout(function() {
$.countdown(selector, datevalue);
}, 1000);
}
};
$.countdown('.date', 1332239568);
Remove datevalue--;
What you're doing now is to count down to a specific epoch time datevalue. But, you are decreasing it by one second each tick. Meanwhile, current time passes in setTimeout so you are both moving in time forward and pulling the target to yourself.
Either keep the original curdate of first call, saved somewhere. Or remove datevalue--;.
Related
I'm creating a countdown element and I want to pass each element to setinterval function to update it each second
var countDownDate = new Date($(this).data('date')).getTime();
setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
$(this).find('.days').text(days);
$(this).find('.hours').text(hours);
$(this).find('.minutes').text(minutes);
$(this).find('.seconds').text(seconds);
}, 1000);
});
thanks in advance
Your questions don't describe what you really wanted, but there are a few ways to access these elements inside the function.
Using an arrow function
setInterval(() => {
/*
your code goes here without changes
*/
}, 1000)
Passing parameters
setInterval(function(days, hours, minutes, seconds) {
/*
your code goes here without changes
*/
days.text(days);
hours.text(hours);
minutes.text(minutes);
seconds.text(seconds);
}, 1000, $(this).find('.days'), $(this).find('.hours'), $(this).find('.minutes'), $(this).find('.seconds'))
Binding
setInterval((function() {
/*
your code goes here without changes
*/
}).bind(this), 1000)
You could just also pass this as an argument.
I would use the arrow function since it's a more modern approach.
I am trying to calculate the point on Earth where the sun is directly overhead using javascript. My function for longitude works great, but I am having trouble with the latitude.
I understand that what I am looking for is called the Solar Declination, and an approximate formula for that is (source):
δ=23.45 * sin[(360/365)(284+N)]
where N is the day in the year, where January 1 is 1, Feb 1 is 32, etc.
This is the function I am using now:
function getSolarDeclination(){
return -23.44 * Math.sin( (360 / 365.25) * getDayOfYear() )
}
function getDayOfYear(){
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var dayOfYear = Math.floor(diff / oneDay);
return dayOfYear + 284
}
However, this doesn't seem to be giving me the right number. For today, June 3, 2020, I am getting 17.607657911890687, whereas at least according to this site it should be ~22.465. This is several hundred miles off!
What am I doing wrong? Is this approximate formula just too "approximate"?
Looks like the formula you're using expects values in degrees, but Math.sin uses radians. If you convert it, it gives roughly the expected result:
function getSolarDeclination(){
return -23.44 * Math.sin( (360 / 365.25) * getDayOfYear() * Math.PI/180 )
}
function getDayOfYear(){
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var dayOfYear = Math.floor(diff / oneDay);
return dayOfYear + 284
}
console.log(getSolarDeclination())
I'm in a brain freeze here.
I have 2 times:
(int) 1815 (18:15) and (int) 1915 (19:15) and I want to calculate the amount of 15 minute blocks between them. (4). How can I approach this in a solid manner?
You could take the minutes of every value and get the delta divided by a quarter hour.
function getMin(t) {
return Math.floor(t / 100) * 60 + t % 100;
}
var a = 1815,
b = 1915,
delta = Math.round((getMin(b) - getMin(a)) / 15);
console.log(delta);
I have this piece of JavaScript code I got from online and I am trying to modify itto countdown to July 10, 2013.
var currentDate = new Date();
$('div#clock').countdown(45 * 40 * 30 * 30 * 1000 + currentDate.valueOf(), function(event) {
July 10, 2013 is about 100 days from now so I want the days to show 100. I don't understand the format of how the date is being passed,I have tried messing around with it but it still doesn't work properly. I tried passing a new Date object to it but that doesn't work either. This code seems to be coming from this jQuery countdown library.
Instead of "45 * 40 * 30 * 30 * 1000" use "100 * 24 * 60 * 60 * 1000" so:
var currentDate = new Date();
$('div#clock').countdown(45 * 40 * 30 * 30 * 1000 + currentDate.valueOf(), function(event) {
would be the final code. That is 1000 (milliseconds/second) * 60 (seconds/minute) * 60 (minutes/hour) * 24 (hours/day) * 100 (days).
There were many similar questions around but none addressed this calculation. Using javascript i it is easy to find the number of milliseconds diff b/w 2 dates for ex:
var mil = Math.floor(new Date("1/1/2012") - new Date("1/7/2012"))
mil is assigned 518400000
to get weeks i would do below
var weeks = mil / (1000*7*24*60*60);
in the above example it exactly fits 1 week. For other possible inputs i would like to get output as ex:
n Weeks, y days , z hours
So i did mil % (1000*7*24*3600) to get the modulus and from the remainder calculate number of days. but astonishingly this was answer i got from console
1 weeks , 6 days seems the week calculated before is also accounted for days again.
How should i calculate these correctly?
var seconds = (mil / 1000) | 0;
mil -= seconds * 1000;
var minutes = (seconds / 60) | 0;
seconds -= minutes * 60;
var hours = (minutes / 60) | 0;
minutes -= hours * 60;
var days = (hours / 24) | 0;
hours -= days * 24;
var weeks = (days / 7) | 0;
days -= weeks * 7;
Assuming mils is non-negative, this leaves mils in the range [0, 1000), leaves minutes and seconds in the range [0, 60), leaves hours in the range [0, 24), and leaves days in the range [0, 7).
There should be 6 days between them, not one week. Your weeks calculation needs to round down:
var weeks = Math.floor(mil / (1000 * 7 * 24 * 60 * 60));
Also, your milliseconds are negative; you want either
var mil = new Date("1/7/2012") - new Date("1/1/2012");
or
var weeks = Math.floor(Math.abs(mil) / (1000 * 7 * 24 * 60 * 60));