Only the text quotes [0] is permanently output, you need random - quotes [0] or quotes [1]. Please help me fix the code.

var quotes=new Array(); quotes[0] = "Сергей оформил товар1."; quotes[1] = "Юлия купила товар2."; var q = quotes.length; var whichquote=Math.round(Math.random()*(q-1)); function showquote(){ document.getElementById('popupname').innerHTML = quotes[whichquote] } function PopUpHide(){ $("#popup1").hide(); } function timeout_init() { showquote(); setTimeout("$('#popup1').show()", 3000); } function timeout_init1() { timeout_init(); setTimeout("$('#popup1').hide()", 8000); } $(document).ready(function(){ PopUpHide(); setInterval(function() { timeout_init1(); }, 10000); }); 
  • Igor: Thank you, but I need to save $ ('# popup1'). Show () and hide (), ie: wait 10 seconds, opens pop-up for 3 seconds, closes after 5 seconds. In this case, the text should be displayed random. - tomas_morgam
  • You are joking? I showed you how to make a randomly selected phrase appear. Your popups and seconds have nothing to do with this. - Igor

1 answer 1

In your code, whichquote is assigned only once.

 var quotes = []; quotes[0] = "Сергей оформил товар 1."; quotes[1] = "Юлия купила товар 2."; var q = quotes.length; function showquote() { var whichquote = Math.round(Math.random() * (q - 1)); document.getElementById('popupname').innerHTML = quotes[whichquote]; } function timeout_init() { showquote(); } function timeout_init1() { timeout_init(); } $(document).ready(function(){ setInterval(timeout_init1, 500); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="popupname"></div>