Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript How to set a delay of 1 second to this js?

chrisj

Bronze Coder
This timer works successfully, but I'd like to add a 1 second delay before it starts, here's the code:

JavaScript:
var Clock = {
  totalSeconds: 0,
  start: function () {
    if (!this.interval) {
        var self = this;
        function pad(val) { return val > 9 ? val : "0" + val; }
        this.interval = setInterval(function () {
          self.totalSeconds += 1;

          document.getElementById("min").innerHTML = pad(Math.floor(self.totalSeconds / 60 % 60));
          document.getElementById("sec").innerHTML = pad(parseInt(self.totalSeconds % 60));
        }, 1000);
    }
 },

  reset: function () {
    Clock.totalSeconds = null;
    clearInterval(this.interval);
    document.getElementById("min").innerHTML = "00";
    document.getElementById("sec").innerHTML = "00";
    delete this.interval;
  },
  stop: function () {
    clearInterval(this.interval);
    delete this.interval;
  }
};
document.getElementById("start").addEventListener("click", function () { Clock.start(); });
document.getElementById("stop").addEventListener("click", function () { Clock.stop(); });

any help is appreciated...

also, upon clicking 'start' this displays 00:00

how can I not show : until it all displays?

and, how can I modify it so that it displays this: 0:00 instead of 00:00 ?

thanks
 
Thanks for your reply.

Here's the html:

HTML:
    <div class="flex1-container">
<span id="min"></span>:<span id="sec"></span>
</div>

And here's my latest unsuccessful attempt at adding the delay code (the last 5 lines) into the js:

JavaScript:
var Clock = {
  totalSeconds: 0,
  start: function () {
    if (!this.interval) {
        var self = this;
        function pad(val) { return val > 9 ? val : "0" + val; }
        this.interval = setInterval(function () {
          self.totalSeconds += 1;

          document.getElementById("min").innerHTML = Math.floor(self.totalSeconds / 60 % 60);
          document.getElementById("sec").innerHTML = pad(parseInt(self.totalSeconds % 60));
        }, 1000);
    }
 },

  reset: function () {
    Clock.totalSeconds = null;
    clearInterval(this.interval);
    document.getElementById("min").innerHTML = "0";
    document.getElementById("sec").innerHTML = "00";
    delete this.interval;
  },
  stop: function () {
    clearInterval(this.interval);
    delete this.interval;
  }
};
document.getElementById("start").addEventListener("click", function () { Clock.start(); });
document.getElementById("stop").addEventListener("click", function () { Clock.stop(); });

document.getElementById("start").addEventListener("click", function () {
    setTimeout(function () {
        Clock.start();
    }, 1000
});

Any suggestions/guidance is welcomed
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom