xxxxxxxxxx
function showMessage() {
console.log("Hello, World!");
}
setInterval(showMessage, 1000);
xxxxxxxxxx
// Define the function to be executed repeatedly
function myFunction() {
console.log('Hello, World!');
}
// Set the interval to execute the function every 2 seconds (2000 milliseconds)
setInterval(myFunction, 2000);
xxxxxxxxxx
// Set the interval time in milliseconds
const intervalTime = 1000; // 1 second
// Create a variable to store the interval instance
let interval;
// Function to be called on each interval
function intervalCallback() {
// Place your code to be executed on each interval here
console.log('Interval callback function called');
}
// Start the interval timer
function startIntervalTimer() {
interval = setInterval(intervalCallback, intervalTime);
}
// Stop the interval timer
function stopIntervalTimer() {
clearInterval(interval);
}
// Example usage:
startIntervalTimer();
// After a certain time, stop the interval timer
setTimeout(() => {
stopIntervalTimer();
}, 5000); // Stop after 5 seconds
xxxxxxxxxx
// variable to store our intervalID
let nIntervId;
function changeColor() {
// check if already an interval has been set up
if (!nIntervId) {
nIntervId = setInterval(flashText, 5);
}
}
function flashText() {
const oElem = document.getElementById("my_box");
if (oElem.className === "go") {
oElem.className = "stop";
} else {
oElem.className = "go";
}
}
function stopTextColor() {
clearInterval(nIntervId);
// release our intervalID from the variable
nIntervId = null;
}
document.getElementById("start").addEventListener("click", changeColor);
document.getElementById("stop").addEventListener("click", stopTextColor);