setInterval
function, which takes in two arguments. The
first argument specifies what function to run automatically. The second
argument specifies how often to run the function, specified in
milliseconds. There are 1000 milliseconds in 1 second. Example of
having a function called doSomething run automatically every second:
setInterval(doSomething, 1000)
clearInterval
function. When calling setInterval, an interval ID is returned, which
can be stored in a variable. When calling clearInterval, pass in the
interval ID. Example:
let intervalId = setInterval(doSomething, 1000) clearInterval(intervalId)
setTimeout
function, which is very similar to setInterval.
Example of having a function called doSomething run after 3 seconds:
setTimeout(doSomething, 3000)
clearTimeout
function, which is very similar to clearInterval. Example:
let timeoutId = setTimeout(doSomething, 3000) clearTimeout(timeoutId)
HTML
<button id="startTimerButton">Start</button> <p id="timeParagraph">Time: 0</p>
JavaScript
let startTimerButton = document.getElementById(`startTimerButton`) let timeParagraph = document.getElementById(`timeParagraph`) let time = 0 startTimerButton.addEventListener(`click`, startTimer) function startTimer() { // your code will appear here } function runTimer() { time = time + 1 timeParagraph.innerHTML = `Time: ${time}` }
Your code
Test your code
Time: 0
HTML
<button id="autoWidthButton">Auto increase width</button>
JavaScript
let autoWidthButton = document.getElementById(`autoWidthButton`) let width = 200 autoWidthButton.addEventListener(`click`, autoIncreaseWidth) function autoIncreaseWidth() { // your code will appear here } function increaseWidth() { width = width + 1 autoWidthButton.style.width = `${width}px` }
Your code
Test your code
HTML
<button id="startCounterButton">Start</button> <button id="stopCounterButton">Stop</button> <p id="countParagraph">Count: 0</p>
JavaScript
let startCounterButton = document.getElementById(`startCounterButton`) let stopCounterButton = document.getElementById(`stopCounterButton`) let countParagraph = document.getElementById(`countParagraph`) let count = 0 let intervalId startCounterButton.addEventListener(`click`, startCounter) stopCounterButton.addEventListener(`click`, stopCounter) function startCounter() { intervalId = setInterval(runCounter, 100) } function stopCounter() { // your code will appear here } function runCounter() { count = count + 1 countParagraph.innerHTML = `Count: ${count}` }
Your code
Test your code
Count: 0
HTML
<button id="autoFontSizeButton">Auto decrease font size</button> <button id="stopFontSizeButton">Stop</button> <p id="fontSizeParagraph">This message will shrink</p>
JavaScript
let autoFontSizeButton = document.getElementById(`autoFontSizeButton`) let stopFontSizeButton = document.getElementById(`stopFontSizeButton`) let fontSizeParagraph = document.getElementById(`fontSizeParagraph`) let fontSize = 18 let autoFontSizeId autoFontSizeButton.addEventListener(`click`, autoDecreaseFontSize) stopFontSizeButton.addEventListener(`click`, stopFontSize) function autoDecreaseFontSize() { autoFontSizeId = setInterval(decreaseFontSize, 500) } function stopFontSize() { // your code will appear here } function decreaseFontSize() { fontSize = fontSize - 1 fontSizeParagraph.style.fontSize = `${fontSize}px` }
Your code
Test your code
This message will shrink
HTML
<button id="delayedMessageButton">Show delayed message</button> <p id="delayedMessageParagraph"></p>
JavaScript
let delayedMessageButton = document.getElementById(`delayedMessageButton`) let delayedMessageParagraph = document.getElementById(`delayedMessageParagraph`) delayedMessageButton.addEventListener(`click`, showDelayedMessage) function showDelayedMessage() { // your code will appear here } function displayMessage() { delayedMessageParagraph.innerHTML = `This is a delayed message` }
Your code
Test your code
HTML
<button id="backgroundColorButton">Change background color after delay</button>
JavaScript
let backgroundColorButton = document.getElementById(`backgroundColorButton`) backgroundColorButton.addEventListener(`click`, changeBackgroundColorAfterDelay) function changeBackgroundColorAfterDelay() { // your code will appear here } function changeBackgroundColor() { backgroundColorButton.style.backgroundColor = `yellow` }
Your code
Test your code