Reputation: 3580
I'm trying to create a pane where stocks quotes are displayed, but that automatically refreshes every 10 seconds and that changes systematically (so have say goog for ten, then aapl and so on.)
Here's what I got.
function stocksUpdate(latest){
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("stocks").innerHTML=xmlhttp.responseText;
}
}
xmlHttp.open("GET","stock.php?latest="+latest+"&x="+Math.random(),true);
xmlHttp.send();
}
function stockShow() {
var symbol = document.getElementById('stocksymbol');
setInterval(stocksUpdate(symbol), 15);
}
}
then
<body onload="stockShow()">
<div id="stocks">
<h3 id="stocksymbol"></h3>
So it should first get the stocksymbol, see that its empty and get the first stock quote, then 15 seconds later get that stock quote and return the next one and so on.
The stock.php page is working perfect, but nothing is displaying. Am I initiating this correctly?? Should I make the first one show then setup the setInterval??
Note that h3 tag is deliberately empty and is accounted for in stock.php so don't be concerned that its empty
Many thanks. Niall
Upvotes: 0
Views: 553
Reputation: 1930
You should replace
setInterval(stocksUpdate(symbol), 15);
with
setInterval(function(){
stocksUpdate(symbol);
}, 15);
As of now you are evaluating the function stocksUpdate(symbol) before passing it to setInterval.
Also, 15 is the time in milliseconds, if you don't want your server to implode, consider raising it to something greater and maybe instead of using setInterval (that could cause 2 responses to arrive in the wrong order) use a setTimeout at the end of the stocksUpdate function
Upvotes: 3