# How to add secounds.?

**URL:** <https://forum.freecodecamp.org/t/how-to-add-secounds/393984>\
**Category:** General\
**Created:** [May 25, 2020, 2:53pm UTC](https://forum.freecodecamp.org/t/how-to-add-secounds/393984 "2020-05-25T14:53:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alphadec](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/alphadec/32/109384_2.png) [@alphadec](https://forum.freecodecamp.org/u/alphadec)\
**Post date:** [May 25, 2020, 2:53pm UTC](https://forum.freecodecamp.org/t/how-to-add-secounds/393984/1 "2020-05-25T14:53:33Z")

</div>

Can anyone say why secounds does not update. ?

```auto
var dato = new Date()

        timer = dato.getHours()
        min = dato.getMinutes()
        sek = dato.getSeconds()

        

        document.getElementById('time').innerHTML = (timer + ":" + min + ":" + sek);

        setTimeout(time, 1000)

```

---

<div class="post-metadata">

**Author:** ![Vojtaa](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/vojtaa/32/150360_2.png) [@Vojtaa](https://forum.freecodecamp.org/u/Vojtaa)\
**Post date:** [May 25, 2020, 2:58pm UTC](https://forum.freecodecamp.org/t/how-to-add-secounds/393984/2 "2020-05-25T14:58:13Z")

</div>

You need to either create a function and move the code in it:

```javascript
function nameOfYourFunction(){
    // code
}
setTimeout(nameOfYourFunction, 1000);

```

Or move whole code in `setTimeout()`:

```javascript
setTimeout(function(){
    // code
}, 1000);

```

EDIT: Also a tip: instead of updating the time every 1s, update it every 0.1s or less (because it takes some time to get and set time, so it will create offset)

---

<div class="post-metadata">

**Author:** ![alphadec](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/alphadec/32/109384_2.png) [@alphadec](https://forum.freecodecamp.org/u/alphadec)\
**Post date:** [May 25, 2020, 3:51pm UTC](https://forum.freecodecamp.org/t/how-to-add-secounds/393984/3 "2020-05-25T15:51:50Z")

</div>

The problem is that if I put it into a function nothing is displayed, so what are I am doing wrong. ?

---

<div class="post-metadata">

**Author:** ![Vojtaa](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/vojtaa/32/150360_2.png) [@Vojtaa](https://forum.freecodecamp.org/u/Vojtaa)\
**Post date:** [May 25, 2020, 3:53pm UTC](https://forum.freecodecamp.org/t/how-to-add-secounds/393984/4 "2020-05-25T15:53:23Z")

</div>

You need to also put `setTimeout(nameOfYourFunction, 1000);` there (edit: NOT in the function, somwhere else). It will call the function.

---

<div class="post-metadata">

**Author:** ![alphadec](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/alphadec/32/109384_2.png) [@alphadec](https://forum.freecodecamp.org/u/alphadec)\
**Post date:** [May 27, 2020, 2:25pm UTC](https://forum.freecodecamp.org/t/how-to-add-secounds/393984/5 "2020-05-27T14:25:28Z")

</div>

Yes I seams to got this working, except that the clock stops after 2 sec. Do you know why. ?

```auto
function t() {

        var dato = new Date()

        timer = dato.getHours()
        min = dato.getMinutes()
        sek = dato.getSeconds()

        

        document.getElementById('time').innerHTML = (timer + ":" + min + ":" + sek)

        

       };

       setTimeout(t, 1000);
       

       t();

```

---

<div class="post-metadata">

**Author:** ![Vojtaa](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/vojtaa/32/150360_2.png) [@Vojtaa](https://forum.freecodecamp.org/u/Vojtaa)\
**Post date:** [May 27, 2020, 2:55pm UTC](https://forum.freecodecamp.org/t/how-to-add-secounds/393984/6 "2020-05-27T14:55:08Z")

</div>

setTimeout does a thing only once, use setInterval for it to repeat it

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:51pm UTC](https://forum.freecodecamp.org/t/how-to-add-secounds/393984/7 "2021-06-01T18:51:56Z")

</div>

Closed on June 1, 2021, 6:51pm UTC


