randomNumMakerInstance

randomNumMakerInstance

我刚刚创建了随机数。我的问题是如何在id中随机获取值,以及如何每5秒自动设置价格变化以及与当前价格相比波动幅度小于+/- 5%,我不知道使用setinterval函数每个人都可以帮助我

我的代码随机价格:

function generateRandomNumber(min,max) {
    return (Math.random() * (max - min) + min).toFixed(2);

};
document.getElementById('price1').innerHTML = generateRandomNumber(0,99.99);
document.getElementById('price2').innerHTML = generateRandomNumber(0,99.99);
document.getElementById('price3').innerHTML = generateRandomNumber(0,99.99);
document.getElementById('price4').innerHTML = generateRandomNumber(0,99.99);
document.getElementById('price5').innerHTML = generateRandomNumber(0,99.99);
document.getElementById('price6').innerHTML = generateRandomNumber(0,99.99);
document.getElementById('price7').innerHTML = generateRandomNumber(0,99.99);

最佳答案

在想出一个更好的解决方案之前,我花了一些时间来解决我遇到的递归问题。

//<![CDATA[
// external.js
function RandNumMaker(min, max, withinPercent, secondsInterval, decimalPlaces){
  this.withinPercent = withinPercent || 5;
  this.secondsInterval = secondsInterval || 5;
  this.decimalPlaces;
  if(decimalPlaces){
    this.decimalPlaces = decimalPlaces;
  }
  var t = this, ia = [];
  function rb(mn, mx){
    return mx-Math.random()*(mx-mn);
  }
  var pn = rb(min, max);
  function rn(){
    var p = 1;
    if(Math.floor(Math.random()*2) === 1)p = -1
    return  p*(Math.floor(Math.random()*t.withinPercent)+1)/100*pn;
  }
  function rf(){
    var r, d = t.decimalPlaces;
    pn += rn();
    if(pn < min || pn > max){
      return rf();
    }
    r = pn;
    if(d)r = (Math.floor(Math.round(Math.pow(10, d+1)*r)/10)/Math.pow(10, d)).toFixed(d);
    return r;
  }
  this.setDiv = function(div){
    div.innerHTML = rf(); ia = [];
    var iv = setInterval(function(){
      div.innerHTML = rf();
    }, this.secondsInterval*1000);
    ia.push(iv);
    return this;
  }
  this.stop = function(){
    for(var i=0,l=ia.length; i<l; i++){
      if(ia[i]){
        clearInterval(ia[i]);
      }
    }
    ia = [];
    return this;
  }
}
var doc, bod, C, E, N, old = onload; // for use on other loads
onload = function(){
if(old)old();
doc = document; bod = doc.body;
C = function(tag){
  return doc.createElement(tag);
}
E = function(id){
  return doc.getElementById(id);
}
N = function(className, inElement){
  var ii = inElement || doc;
  var cN = ii.getElementsByClassName(className);
  if(cN){
    return cN;
  }
  var tn = ii.getElementsByTagName('*');
  for(var i=0,e,a=[],l=tn.length; i<l; i++){
    e = tn[i];
    if(e.className && e.className === className)a.push(e);
  }
  return a;
}
var sr = new RandNumMaker(0, 99.99), sl = N('slow');
var fr = new RandNumMaker(0, 99.99), fs = N('fast');
sr.decimalPlaces = 2;
fr.secondsInterval = 0.1; fr.withinPercent = 5;
for(var i=0,l=sl.length; i<l; i++){
  sr.setDiv(sl[i]);
}
for(var i=0,l=fs.length; i<l; i++){
  fr.setDiv(fs[i]);
}
}
//]]>
/* extrnal.js */
html,body{
  padding:0; margin:0;
}
body{
  background:#000; overflow-y:scroll;
}
.main{
  width:940px; background:#fff; padding:20px; margin:0 auto;
}
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
  <head>
    <meta http-equiv='content-type' content='text/html;charset=utf-8' />
    <meta name='viewport' content='width=device-width' />
    <title>test</title>
    <link type='text/css' rel='stylesheet' href='external.css' />
    <script type='text/javascript' src='external.js'></script>
  </head>
  <body>
    <div class='main'>
      <div class='slow'></div>
      <div class='slow'></div>
      <div class='slow'></div>
      <div class='slow'></div>
      <div class='slow'></div>
      <div class='slow'></div>
      <div class='slow'></div>
      <hr />
      <div class='fast'></div>
      <div class='fast'></div>
      <div class='fast'></div>
      <div class='fast'></div>
    </div>
  </body>
</html>


现在应该工作!

创建new RandNumMaker(min, max)之后,您可以使用简单分配来设置randomNumMarkerInstance.decimalPlacesrandomNumMakerInstance.withinPercentrandomNumMakerInstance.secondsIntervalrandomNumMakerInstance.setDiv(div)启动它。 randomNumMakerInstance.stop()停止它。

09-26 16:22