Syntax is Below:
setInterval(() => { console.log(`Hi There!`); }, 4000); // Console.logs "Hi There" every 4 seconds (4000 milliseconds) or setInterval(function(){ console.log(`Hi There!`); }, 8000); // Console.logs "Hi There" every 8 seconds (8000 milliseconds)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../styles.css">
<title>JavaScript: setInterval()</title>
</head>
<body>
<h1>JavaScript: setInterval()</h1>
<input type="number" name="number" id="number" placeholder="Enter Interval in Seconds">
<button id="submit">Submit</button>
<p>Syntax is Below:</p>
<pre>
setInterval(() => {
console.log(`Hi There!`);
}, 4000);
// Console.logs "Hi There" every 4 seconds (4000 milliseconds)
or
setInterval(function(){
console.log(`Hi There!`);
}, 8000);
// Console.logs "Hi There" every 8 seconds (8000 milliseconds)
</pre>
<div id="fill-div"></div>
<script>
const submit = document.getElementById('submit');
const fillDiv = document.getElementById('fill-div');
const number = document.getElementById('number');
function Interval(seconds){
const milliseconds = seconds * 1000;
setInterval(() => {
console.log("Hi There");
console.log(milliseconds);
fillDiv.innerHTML = fillDiv.innerHTML + `Interval Selected: ${seconds} Seconds<br>`
}, milliseconds);
}
submit.addEventListener('click', () => {
Interval(number.value);
});
</script>
<?php
include('../show_code.php');
show_code('index.php');
?>
</body>
</html>