8/24/2023 0 Comments Javascript countdown timer![]() So, slice(-2) returns the last two characters of a string. A negative value returns the targeted characters rather than slicing them off. ![]() Similarly, slice(2) removes the first two characters and returns the rest. For instance, slice(1) slices off the first character of a string and returns the rest of the string. This can be achieved with a formatting trick that makes use of the slice() method that can be used to return a portion of a data set. Although this is not necessary, the timer looks better when the number of digits doesn’t change all the time on the screen. The code above adds a “0” character to hours, minutes, and seconds when their values are less than 10. You can place content into the targeted HTML elements with the innerHTML property.Īdd the following code to the if (t >= 0) block, below the time conversion calculations:ĭocument.getElementById("timer-days").innerHTML = days +ĭocument.getElementById("timer-hours").innerHTML= ("0" + hours).slice(-2) +ĭocument.getElementById("timer-mins").innerHTML= ("0" + mins).slice(-2) +ĭocument.getElementById("timer-secs").innerHTML= ("0" + secs).slice(-2) + The getElementById() method of the document object allows you to target the HTML elements you created in Step 1, respectively #timer-days, #timer-hours, #timer-mins, and #timer-secs. Now that you have all the data in the right format, you can output the timer to the screen.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |