-1

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;
    }
Kijewski
  • 25,517
  • 12
  • 101
  • 143
Donald
  • 27
  • 5
  • 1
    Actually what do you want to achieve? Create countdown for 3 minutes? – Mr.Cocococo Jun 10 '14 at 23:31
  • it does what I need, im tring to get it to put 3m 00s instead of things like 180s if I add minutes, it will say 2m 179s – Donald Jun 10 '14 at 23:32
  • let me rephrase, im tring to get it out output correctly, – Donald Jun 10 '14 at 23:34
  • It seems this is what you need: http://stackoverflow.com/questions/625433/how-to-convert-milliseconds-to-x-mins-x-seconds-in-java?rq=1 – MythThrazz Jun 10 '14 at 23:35
  • I honestly read that post already, not sure what version of javascript im using, but this program also has c++ functions I have found out. So what im trying to do is, is after it calculates the total in milliseconds and I have done the math already to get that to seconds, i need to somehow cause the seconds to not go past 60 but keep it cycling. – Donald Jun 10 '14 at 23:41
  • What is your setup? What kind of framework do you use? Webkit, XUL? Your question seems to lack a lot of information. – Kijewski Jun 10 '14 at 23:45
  • there is no library, not framework, nothing like that that I am aware of, also, I just got an error line 16 time_in_seconds is not defined. – Donald Jun 10 '14 at 23:51

1 Answers1

2

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.

Kijewski
  • 25,517
  • 12
  • 101
  • 143
  • i found this posted in many places, i tried it various way, I will not respond to % symbols, – Donald Jun 10 '14 at 23:37
  • everytime I put % in the equation, it just sits there or kicks back and syntax error. – Donald Jun 10 '14 at 23:42
  • keep in mind I am teaching myself and only been doing this for a week. – Donald Jun 10 '14 at 23:43
  • 1
    Did you try it with the % exactly the way he did it? It should not give any errors. This answer is completely correct. – rp.beltran Jun 10 '14 at 23:55
  • i could have sworn I tried this already, i put it in a different spot this time, it is working, I have no idea how i got it wrong the first time. now the question is do I need one for the minutes or anything? – Donald Jun 11 '14 at 00:09
  • ok, I just tested it, Kay you rock, sorry I let my first failure with it cause me to doubt the answer, if you have any resources online I can look at please feel free to throw them at me, I am a sponge looking for good stuffs to soak up. – Donald Jun 11 '14 at 00:13