Basically I have a random number generator that increases or decreases every second. I want this to continue even if no one is on the page.
So when I leave maybe its 90, then I come back maybe it's 120 or 60. Not sure how to go about this. I want this value to fluctuate permanently.
I currently have set interval setup, but I am not sure if this will function while no one is on the page, and I am sure it don't.
So how can I have this function running continuously in order to manipulate the data.
myFakeStock = [{
name: "Test1",
startPrice: 90.00,
shares: 1000
},
{
name: "Test2",
startPrice: 30.00
}]
console.log(myFakeStock.startPrice)
var generatePrice = setInterval(randomPriceGenerator, 1000)
startPrice = myFakeStock[0].startPrice
outputPrice = document.getElementById('price')
let lastPrice = []
function randomPriceGenerator() {
min = -.01
max = .01
randomMultiplier = Math.random() * (max - min) + min
if(randomMultiplier > .002) {
randomMultiplier = randomMultiplier + .002
randomMultiplier = randomMultiplier.toFixed(2)
}
else if(randomMultiplier < 000) {
randomMultiplier = randomMultiplier + -.001
randomMultiplier = randomMultiplier.toFixed(2)
}
else
{
randomMultiplier = randomMultiplier.toFixed(2)
}
priceFlux = startPrice * randomMultiplier
newPrice = priceFlux + startPrice
console.log(newPrice)
startPrice = newPrice
lastPrice.push(startPrice)
console.log(lastPrice)
percChange = startPrice / newPrice - 1
outputPrice.innerHTML = myFakeStock[0].name +"<br>$" + newPrice.toFixed(2) + "<br>" + percChange.toFixed(2)
var d = new Date()
var n = d.getMinutes()
var dps = [] // dataPoints
var chart = new CanvasJS.Chart("chartContainer", {
easing: 'easeOutBounce',
theme: "light2",
zoomEnabled: true,
title :{
text: "Dynamic Data"
},
data: [{
type: "line",
dataPoints: dps
}
]
})
var xVal = n;
var yVal = newPrice ;
var updateInterval = 1000
var dataLength = lastPrice.length // number of dataPoints visible at any point
var updateChart = function (count) {
for (var j = 0; j < count; j++) {
yVal = lastPrice[j]
dps.push({
x: xVal,
y: yVal
})
xVal++
}
chart.render()
}
updateChart(dataLength)
setInterval(function(){updateChart()}, updateInterval)
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test Page</title>
</head>
<body>
<p id = "price">Random Number Fluxuaction</p>
<canvas id="myChart" width="400" height="400"></canvas>
<div id="chartContainer" style="height: 300px; width: 100%;"></div>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<script src="script.js"></script>
</body>
</html>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…