I have been trying to create a function that generates a random number but I want this number to change every 5 minutes. Below is the code that I have tried:
function randNum(){
let randomNumber = Math.floor(Math.random() * 10);
const paragraphNew = document.createElement("p");
const watchNow = document.createTextNode(randomNumber + " are viewing this product now.");
paragraphNew.appendChild(watchNow);
const element1 = document.getElementById("cart-container");
element1.appendChild(paragraphNew);
}
When I try using the setInterval function like this:
let randomNumber = setInterval(Math.floor(Math.random() * 10), 5000);
I always get 1 as a result. I have also tried calling the function like this:
setInterval(randNum, 5000);
The problem is that a new paragraph is getting added everytime, without deleting the last one.
How can I make it work so I can generate the same number that lasts 5 minutes even after I refresh the page?