0

I am trying to make a timer start counting down. I think my next step is to make the countdown function to actually work. What would be the best approach for this?

JavaScript and HTML codes below:

// Set the default timer as 25:00
var timer = document.getElementById("mytime");
var counter = setInterval(function(){
  countdown()}, 1000);
//user clicks the 'start' button and timer starts counting down

function countdown(minutes, seconds){ 
  timer.value = "17:30:00"; //default value
  document.getElementById("btn").innterHTML = counter;
  counter--;
};


var click = document.getElementById("btn");
btn.addEventListener("click", countdown); //"click" as DOM Object?
btn.addEventListener("click", stopcounting);
  
<head> 
  <title>Pomodoro Timer</title>
</head>

<body>
  <h1>POMODORO TIMER</h1>
    <div id="main">
      <input type="time" id="mytime">
        <button id="btn"> start </button>
      </div>
</body>

6
  • your code makes absolutely no sense. Commented Dec 2, 2015 at 7:03
  • why would you create your own timer if there is a jQuery package that you can use, dont re-invent the wheel. see this link: http://hilios.github.io/jQuery.countdown/ Commented Dec 2, 2015 at 7:39
  • @r3wt you comment is absolutely helpful. Commented Dec 2, 2015 at 14:44
  • @jso1226 its clear you have no idea what you are doing. stack overflow is not a place for posers to get free work. Commented Dec 2, 2015 at 21:53
  • @r3wt I did my research before asking and I clearly don't know what I'm doing so I needed help. Commented Dec 2, 2015 at 23:21

4 Answers 4

1

This is my own version of the clock where you can start the timer by adding the specific time when the event happened. Please see the below code.

// Set the date we're counting down to
var start_at = new Date('2020-06-29 12:00:00');

// Update the count down every 1 second
var x = setInterval(function() {

// Get todays date and time
var now = new Date();

// Find the distance between now an the count down date
var distance = now - start_at;


// 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);

// Output the result in an element with id="demo"
document.getElementById("demo").value = hours + "h "
+ minutes + "m " + seconds + "s ";

}, 1000);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for helping out our community patron. Make sure you do not include unnecessary formatting unless required. For more help refer this thread: stackoverflow.com/help/formatting
0

This may be what you want, I haven't done it in the date time format but you can pretty much convert that by yourself.

<head>
  <title>Pomodoro Timer</title>
</head>
<body>
  <h1>POMODORO TIMER</h1>
    <div id="main">
      <input id="mytime" value=1000>
        <button id="btn"> start </button>        
      </div>
</body>
<script>
  var input = document.getElementById("mytime");
  var button = document.getElementById("btn");
  var started = false;
  var timer;
  var startTimer = function (count) {
    return setInterval(function () {
      input.setAttribute("value", --count);
    }, 1000);
  }
  button.addEventListener("click", function () {
    if (!started) {
      var count = parseInt(input.getAttribute("value"));
      timer = startTimer(count);
      started = true;
      button.innerHTML = "stop";
    } else {
      clearInterval(timer);
      started = false;
      button.innerHTML = "start";
    }
  });
</script>

JSFiddler: https://jsfiddle.net/m7pev0vm/

Comments

0

Your code is a bit jumbled. So let me help you sort it out.

First off, the click function needs to trigger a the start of countdown.

The counter in the timer function would need to be something in seconds. So that you can keep decrementing the value while on the face of it, it would be in hours, minutes & seconds.

Also, You need to have a target time. So if I say counter until 00:00:00 then the following could work, as an example :

// Set the default timer as 5:30 PM
var timer = document.getElementById("mytime");
var counter = (17*60*60) + (30*60);
timer.value = "17:30:00"; //default value

function countdown(minutes, seconds){ 
  --counter;
  var hrs = Math.floor(counter / 3600),
      min = Math.floor((counter % 3600) / 60),
      sec = Math.floor((counter % 3600) % 60);
  
  timer.value = hrs + ":" + min + ":" + sec;
};

function onClick() {
    var counter = setInterval(function(){
        countdown();
    }, 1000);
    //user clicks the 'start' button and timer starts counting down
}

var click = document.getElementById("btn");
btn.addEventListener("click", onClick); //"click" as DOM Object?
btn.addEventListener("click", stopcounting);
  
<head> 
  <title>Pomodoro Timer</title>
</head>

<body>
  <h1>POMODORO TIMER</h1>
    <div id="main">
      <input type="time" id="mytime">
        <button id="btn"> start </button>
      </div>
</body>

Comments

0

I know this is coming very late but I know it will help someone someday.

<!DOCTYPE HTML>
<html>
<head>


</head>
<body>

<form>
   
  <input type="text" name="demo" id="demo" size="20"  readonly="true" style="text-align:center;"/>
  <br>
 
</form> 


<script>
// Set the date we're counting down to
var countDownDate = new Date().getTime() + ((1)*60*60*1000);

// Update the count down every 1 second
var x = setInterval(function() {

    // Get todays date and time
    var now = new Date().getTime();
    
    // Find the distance between now an the count down date
    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);
    
    // Output the result in an element with id="demo"
    document.getElementById("demo").value = hours + "h "
    + minutes + "m " + seconds + "s ";
    
	
	// If the count down remains 15 minutes, write some text
	 
if (minutes == 59 && seconds == 1) {
        alert("Hello! 1 minute gone");
    }
     
    
    // If the count down is over, write some text
    if (distance < 0) {
        clearInterval(x);
        document.getElementById("demo");
        demo.value= "EXPIRED";
    }
}, 1000);
</script>

</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.