This appears in the middle of a function, I wrote it myself and im stuck.
What it does it make a call for the time in milliseconds, (countStart) and then it makes a call for the time when it completes (countEnd). When this is activated, it looks at the countStart countEnd and does the math to determine if it has run long enough to run again.
All of this is successful, except, it displays everything in seconds, so 3 minutes is 180 seconds. I am trying to figure a way to make it count down 3 minutes correctly.
Is this a feasible thing? I have done hours of research and I can find 50 ways to supposedly do this, and all of them are HTML based and massively large. I tried to add a if this goes above 60 function then add 1 to minutes and subtract 60 from seconds, but that locked up the counter.
So anyway any help or guidance would be appreciated
var countStart = 100000000000000;
var countStop = 100000000000020;
var countDelay = 180000;
alert(parseInt((factor * -1) / 1000, 10) + " s)");
for (var factor = countStop - (countStart + countDelay) + 1; factor > 0; factor--){
countStart = 0;
countStop = 0;
countDelay = 0;
}
Do the math:
var time_in_seconds = ...;
var minutes = (time_in_seconds / 60) | 0; // divide by 60 and truncate to an integer.
var seconds = time_in_seconds % 60; // this is the modulo operator
If you don't like the modulo operator for no obvious reason, then you can do further math:
var minutes = (time_in_seconds / 60) | 0;
var seconds = time_in_seconds - minutes * 60;
To format the two integers:
var string = minutes + "m" + (seconds < 10 ? "0" + seconds : seconds);
condition ? then_expr : else_expr is the ternary operator. If the condition is true, then then_expr gets evaluated, otherwise else_expr.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to come up with a way to get an array of times based on it's start hour, end hour, and duration in minutes.
For example:
Start Hour: 9
End Hour: 17
Duration: 15
I successfully have done this by looping over the start and end hour and then have a loop inside that loop to calculate the minutes.
function getTimesArray(start, end, length) {
var time_array = [];
var duration = length / 60;
var count = 0;
for (var i = start, len = end; i < len; i++) {
var minutes = 1 / duration;
for (var b = 0, leng = minutes; b < leng; b++) {
var min = length * b;
time_array[count] = i + ':' + (min == 0 ? "00" : min);
count ++;
}
}
time_array[time_array.length+1] = i + ':' + "00";
return time_array;
}
https://jsfiddle.net/wvtpqx8c/
The issue with this is that you can only put in a duration of 60 minutes max because it always loops over the hours.
What I need is to be able to calculate the times no matter how many minutes the length is. For example, if the length is 120, it should show every 2 hours from start to end hours.
This will be a bit easier if you think of everything in terms of minutes and calculate the hours:minutes when you make the string. This will let you use a simple while loop rather than a complicated for loop with conditions. Then you can just add the minutes to the start time and end when it's greater or equal to the end time:
function getTimesArray(start, end, length) {
let startMin = start * 60
let endMin = end * 60
let times = []
while (startMin <= endMin){
let mins = startMin % 60
let hours = Math.floor(startMin / 60)
let timeString = hours.toString() + ":" + mins.toString().padStart(2, '0')
times.push(timeString)
startMin += length
}
return times
}
console.log(getTimesArray(1,6, 12))
console.log(getTimesArray(4,8, 110))
This doesn't include the end if the duration overshoots it as in the second example above as this would add a duration different than length. It would be a simple thing to push one more time on if you need that, however.
I am trying to make a small question/answer quiz game using react, and I want to show a timer that counts down every second. Each game will last 10, 15, or 30 minutes at most, so I want to show a timer that updates every second in the bottom of the screen (in big font, of course!), something like 15:00, 14:59, 14:58, and so on until it hits 00:00.
So, given a start time such as 2016-04-25T08:00:00Z, and an end time after adding 15 min of 2016-04-25T08:15:00Z, I want to start the countdown.
My issue is that I am not understanding how to use setIntervals to keep calling my method to find the remaining time.
timeLeft = Math.round(timeLeft/1000) * 1000;
const timer = new Date(timeLeft);
return timer.getUTCMinutes() + ':' + timer.getUTCSeconds();
EDIT: You've edited your question. You will need the time padding, and the method below will be faster than what you are using, but to answer your question about setInterval:
First, define your function to run your timer and decrement each time it's called:
var timeLeft; // this is the time left
var elem; // DOM element where your timer text goes
var interval = null; // the interval pointer will be stored in this variable
function tick() {
timeLeft = Math.round(timeLeft / 1000) * 1000;
const timer = new Date(timeLeft);
var time = timer.getUTCMinutes() + ':' + timer.getUTCSeconds();
elem.innerHTML = time;
timeLeft -= 1000; // decrement one second
if (timeLeft < 0) {
clearInterval(interval);
}
}
interval = setInterval(tick, 1000);
OG Answer:
No, I do not believe there is a built-in way to display time differences.
Let's say you have two date objects:
var start = Date.now();
var end = Date.now() + 15 * 60 * 1000; // 15 minutes
Then you can subtract the two Date objects to get a number of milliseconds between them:
var diff = (end - start) / 1000; // difference in seconds
To get the number of minutes, you take diff and divide it by 60 and floor that result:
var minutes = Math.floor(diff / 60);
To get the number of seconds, you take the modulus to get the remainder after the minutes are removed:
var seconds = diff % 60;
But you want these two padded by zeros, so to do that, you convert to Strings and check if they are two characters long. If not, you prepend a zero:
// assumes num is a whole number
function pad2Digits(num) {
var str = num.toString();
if (str.length === 1) {
str = '0' + str;
}
return str;
}
var time = pad2Digits(minutes) + ':' + pad2Digits(seconds);
Now you have the time in minutes and seconds.
I was having some problem when trying to perform some calculation logic using JavaScript. Basically along a route there is 80 steps and it took around 9 minutes to finish up the entire route.
So I was trying to do an auto route which will tell you the minutes left to destination. My logic is as below:
9 * 60 / 80 = 6.75
So basically for each step is 6.75 seconds but I wanted to show a round number like 9 instead of 8.4 minutes. Here is the code:
getAllBusLoc(function(busList) {
var totalBusLoc = busList.length;
var busLeftPct = Math.round(parseFloat(busList.length) * 40 / 100)
document.getElementById("busStopLeft").innerHTML = totalBusLoc;
pointArr.forEach(function(coord,index){
setTimeout(function(){
var travelTime = document.getElementById(travelDistMin").value;
moveNext(coord.x, coord.y);
}, 1000* index);
});
});
I got the travel time as variable travelTime which in this case is 9 minutes. For each point, I wanted to minus 6.75 seconds from the 9 minutes and display a round number instead of 8.2.
Any ideas?
Thanks in advance.
Use Math.round() for subtracting 6.75 from travelTime.
This is will round to the nearest whole number.
An idea that I could suggest is to write a generic function that transforms a decimal time interval (for example, 8.25 minutes) into its equivalent 'mm:ss' value instead of rounding so that you display the precise time representation:
Number.prototype.toMMSS = function () {
d = this;
var sign = d < 0 ? "-" : "";
var min = Math.floor(Math.abs(d))
var sec = Math.floor((Math.abs(d) * 60) % 60);
return sign + (min < 10 ? "0" : "") + min + ":" + (sec < 10 ? "0" : "") + sec;
};
Example:
8.25.toMMSS() // "08:15"
JSFiddle
Or, you could try the moment plugin duration function like:
moment.duration(8.25, 'minutes').minutes(); // 8
Or, the humanize method to round off:
console.log(moment.duration(8.51, "minutes").humanize()); // "9 minutes"
console.log(moment.duration(8.15, "minutes").humanize()); // "8 minutes"
I started working on a timer to show how long people have been on my page. The problem is that instead of counting the seconds, it just keeps adding zeros to the end. Can anyone show me where I went wrong?
<script language="javascript">
<!--
var seconds = 0
var minutes = 0
document.getElementById('timer').innerHTML = '0'
function Timer() {
if ( seconds < 10 ) {
seconds = "0" + seconds
}
else if ( minutes < 10 ) {
minutes = "0" + minutes
}
else if ( seconds >= 59 ){
seconds = 0
minutes += 1
}
else
seconds += 1
document.getElementById('timer').innerHTML = "You've been on my blog for "+minutes+" : "+seconds+" minutes."
setTimeout("Timer()",1000)
}
//-->
</script>
Well yes, because you're concatenating a string. The + operator does both concatenation and addition.
"0" + foo // concatenate the string "0" and foo
0 + foo // add foo to 0 (given that both are numbers)
You're going about the timer the wrong way though. Your script is not guaranteed to execute on time, any slight delay in execution will give you accumulative the wrong value. The right way to do a timer is to take a fixed start time and use that as the base to calculate the difference every second or so.
var start = Date.now();
function timer() {
alert(((Date.now() - start) / 1000) + ' seconds elapsed');
}
setInterval(timer, 1000);
You're switching the variable type for seconds from being:
var seconds = 0 // integer type
....
seconds = "0" + seconds // now seconds is holding a string!
seconds (and minutes) should only be integers.
Also, just focus on the seconds. Later, divide it by 60 to display number of minutes. seconds % 60 will be the remaining number of sec.
I'm currently using the following function which is based on an example from the web, it is called every second to display the current progress through a video stream.
Is there something I could do to make this more efficient?
function secondstominutes(secs){
var s;
if(secs > 60){
var min = Math.floor(secs / 60);
s = min < 10 ? "0" : "";
s += min +":";
secs = secs - min * 60;
} else {
s = "00:";
}
if(secs < 10){
s+= "0" + Math.floor(secs);
} else {
s += Math.floor(secs);
}
return s;
}
function secondstominutes(secs)
{
var mins = Math.floor(secs / 60);
secs = secs % 60;
return (mins < 10 ? "0" + mins : mins)
+ ":"
+ (secs < 10 ? "0" + secs : secs);
}
Yes it can be a little simpler, and uses less Math.floor, local variables etc. Here mine proposition:
function secondstominutes(secs)
{
return (Math.floor(secs/60))+":"+secs%60;
}
This will give result like:
0:1 for 1 sec
0:10 for 10 sec
1:1 for 61 sec
etc.
If you want spaces etc, it could be done like this:
function formatZero(number)
{
return (number>9) ? number : "0"+number;
}
function secondstominutes(secs)
{
return formatZero((Math.floor(secs/60)))+":"+formatZero(secs%60);
}
And this one is for obbsesive One-line function ppls ;)
function secondstominutes(secs)
{
return ((arguments[1]=(Math.floor(secs/60)))<10?"0":"")+arguments[1]+":"+((arguments[2]=secs%60)<10?"0":"") + arguments[2];
}
bobwienholts version is compact, fast and clear and it's very similar to the way I would have implemented it. For fun I did some profiling of a few different solutions and I found this to be a little faster, however it is not as clear and do an assignment semi hidden in a statement:
function secondstominutes(secs)
{
var m = (secs / 60) | 0;
return (m < 10 ? "0" + m : m)
+ ":"
+ ( ( secs %= 60 ) < 10 ? "0" + secs : secs);
}
Explanation:
var m = (secs / 60) | 0;
Divide secs by 60 and binary "OR" the result with 0. The binary "OR" converts the value to an integer and by or-ing with 0 the integer part of the result is returned without modification. This is faster than calling Math.floor() but less clear. Since it is converted to an integer you limit the length of any movie to 2147483647 minutes ≈ 35791394 hours ≈ 1491308 days ≈ 4085 years.
( secs %= 60 )
This is the short form of doing (secs = secs % 60). It divides the value of secs by 60 and assigns secs the reminder (example 61 % 60 = 1). By profiling I found that it was a little faster to put that computation inside the return statement instead of on a row of its own.
(m < 10 ? "0" + m : m)
This computes the first statement m < 10 and if it is true the second statement is executed "0" + m else the third: m. In English: If m is less than 10 then a zero is added at the beginning else the value is returned as is.