How to put as many js on one page

var random = 1000; setInterval(function() { random = randomizator(60000, 200000); }, 1000); setInterval(function() { showRandom(random); }, random); function randomizator(a, b) { return Math.floor(Math.random() * b) + a; } function showRandom(random) { $('#test').text(random); } 
 <div id="test"></div> 

Replacing id with class did not work, but I could not do it right.

  • 2
    "how to place as many js as you like" - what does this mean? xyproblem.info - Igor
  • In the current view, you have one interval for one div . Run in for example, by the number of divs in which there should be values. After all, as I understand it, all fields should have their random values? - Moonvvell

2 answers 2

 var code = "var random=1000;setInterval(function(){random=randomizator(60000,200000)},1000);setInterval(function(){showRandom(random)},random);function randomizator(a,b){return Math.floor(Math.random()*b)+a}function showRandom(random){$('#test').text(random)}"; for (var i=0; i < 4; i++) { eval(code) } 
  • Well, look, I insert into the form <div id = "test"> (a script on setinterval and getrandom) </ div> close it and that's how I need to do well 15 times, let's say. and it is done once and everything. - Henrry Kavel
  • Yes that's right. well, or somehow replace the id with the class or something ... just this script I can use once on the page (and I need to allow 5 numbers on the page to be generated at different intervals in time. from 5 scripts) - Henrry Kavel
  • Moonvvell THANK YOU VERY MUCH! everything is super! very much helped out! thank! - Henrry Kavel 2:58 pm
  • @HenrryKavel you wouldn’t have written a “thank you” - but noted the answer with such a green tick ... - Pavel Mayorov

Those. Do you want to add 10 div to the example and connect this script to everyone so that it works in all elements and sets different values?

 <div id="test" class="qqq"></div> <div id="test2" class="qqq"></div> <div id="test3" class="qqq"></div> var random = 1000; $('.qqq').each(function () { var element = $(this); setInterval(function () { random = randomizator(60000, 200000); }, 1000); setInterval(function () { showRandom(random, element); }, random); }) function randomizator(a, b) { return Math.floor(Math.random() * b) + a; } function showRandom(random, element) { element.text(random); } 

Feeddle

  • and why, if we reduce the interval, let's say from 1 to 8, will it write the same numbers? those. more like all different things are all random, but in small intervals they repeat exactly one another. - Henrry Kavel 4:16 pm