Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
How do I display a given random array element every two seconds until it is stopped?

I am trying to make a program that randomly displays one of four defined emojis given in an array when the button is clicked and switches every two seconds continues to do so until the stop button is clicked. Note that I have not finished the stop function yet as I cannot work out why my randomEmoji function is not displaying anything. Thanks in advance :).

var display = document.getElementById("emojiDisplay");
var emojiList = ["🄳", "🤩", "šŸ‘¾", "😵"];

function randomEmoji() {
  emojiDisplay.innerHTML = emojiList[Math.floor(Math.random() * 
 emojiList.length)];
 setInterval(function() {
  document.getElementById("emojiDiplay").innerHTML = emojiList[i++];
  if (i == emojiList.length) i = 0;
}, 2000);
   }





function stop() {

}
<button onclick=randomEmoji()>Display random emoji</button>
<button onclick=stop()>Stop</button>
</br>
</br>
<div id="emojiDisplay">
</div>

about 4 years ago Ā· Juan Pablo Isaza
1 answers
Answer question

0

Based on your idea and your code, I’ve updated it and let it work (the stop function works, too). You can check the below demo:

var display = document.getElementById("emojiDisplay");
var emojiList = [ "🄳", "🤩", "šŸ‘¾", "😵" ];
var i = 0;
var timer;

function randomEmoji() {
  clearInterval(timer);

  // Call show Emoji to let it shows instanly.
  showEmoji();

  // Put showEmoji function to let it repeats
  timer = setInterval(function() {
    showEmoji();
  }, 2000);
}

function showEmoji() {
  i = Math.floor(Math.random() * emojiList.length);
  emojiDisplay.innerHTML = emojiList[i];
}

function stop() {
  // clear interval timer to let it stops
  clearInterval(timer);
}
<!DOCTYPE html>
<html>

<head>
  <title>EmojiRandomiser</title>
</head>

<body>
  <button onclick=randomEmoji()>Display random emoji</button>
  <button onclick=stop()>Stop</button>
  <br>
  <br>
  <div id="emojiDisplay">
  </div>
</body>

</html>

about 4 years ago Ā· Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
Ā© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!