PDA

View Full Version : Looking to speed up script process



at51178
01-14-2008, 06:57 PM
Hi

Description of my script:
The script uses the ActiveXObject("Microsoft.XMLHTTP") to request information from various sites and display the information into one page.

The site looks up stock quote information so since I plan on looking up hundreds of stock quotes I looped the functions so that it would lookup the symbols from an array and retrieve the information.

Issue:
The script works fine, the only problem is that it takes too long, and I would like to see if I can somehow decrease the time it takes.

Here is the code

HTML Code***********
<html>
<head>
<script src = "getdata.js">
</script>
</head>
<body>
<a href = "javascript:getdata()">Get Data</a>
<table border = 1 cellspacing = 0 cellpadding = 0 bordercolor = '#c2d69a'>
<tr align = 'center'>
<td nowrap width = '40px'>Order</td>
<td nowrap width = '60px'>Symbol</td>
<td nowrap width = '200px'>Company</td>
<td nowrap width = '60px'>Price</td>
<td nowrap width = '60px'>Change</td>
<td nowrap width = '150px'>Barchart Overall Average</td>
<td nowrap width = '150px'>Stock Scouter Rating</td>
<td nowrap width = '100px'>Caps Rating</td>
<td nowrap width = '50px'>Status</td>
</tr>
</table>
<div id = "dv_content2">
</div>
</body>
</html>



JavaScript Code********************
var req = new ActiveXObject("Microsoft.XMLHTTP")
var vr_symbol = "DIOD,HAL,GOOG,BRCM"
var vr_symbol_arry = vr_symbol.split(",")
var vr_content = ""

function getdata()
{
for(var i = 0; i < vr_symbol_arry.length; i++)
{
req.open("GET","http://www2.barchart.com/lookup.asp?sym=" + vr_symbol_arry[i],false)
req.onreadystatechange = function()
{

if(req.readyState == 4)
{
if(req.responseText.indexOf("bcMLink>" + vr_symbol_arry[i] + "</a>") > 0 )
{
get_company_info(i)
}
else
{
vr_content = vr_content + "<table border = 1 cellspacing = 0 cellpadding = 0 bordercolor = '#c2d69a'><tr align ='center'><td nowrap width = '40px'>" + i + "</td><td nowrap width = '60px'>" + vr_symbol_arry[i] + "</td><td nowrap width = '200px'> N/A </td><td nowrap width = '60px'>N/A</td><td nowrap width = '60px'>N/A</td><td nowrap width = '150px'>N/A</td><td nowrap width = '150px'>N/A</td><td nowrap width = '100px'>N/A</td><td nowrap width = '50px'>Not In DB</td></tr></table>"
document.getElementById("dv_content2").innerHTML = (vr_content)
}

}
}
req.send(null)
}
}


function get_company_info(i)
{
req.open("GET","http://finance.google.com/finance?q=" + vr_symbol_arry[i],false)
req.onreadystatechange = function()
{

if(req.readyState == 4)
{
//Get Company Name
var vr_from = req.responseText.substring (req.responseText.indexOf("latest on"),req.responseText.length)
var vr_to = vr_from.indexOf("(")
var vr_company = vr_from.substring(10,vr_to)

//Get Current Price
var vr_from = req.responseText.substring (req.responseText.indexOf('class="pr" id="ref_'),req.responseText.length)
var vr_to = vr_from.indexOf("</span>")
var vr_price = "$" + vr_from.substring(vr_from.indexOf('">') + 2,vr_to)

//Get Price Change
var vr_from = req.responseText.substring (req.responseText.indexOf('class="chg"'),req.responseText.length)
var vr_to = vr_from.indexOf("</span>")
var vr_change = vr_from.substring(vr_from.indexOf('">') + 2,vr_to)

get_barchart_rating(i,vr_company,vr_price,vr_change)
}
}
req.send(null)
}

function get_barchart_rating(i,vr_company,vr_price,vr_change)
{
req.open("GET","http://quote.barchart.com/texpert.asp?sym=" + vr_symbol_arry[i],false)
req.onreadystatechange = function()
{

if(req.readyState == 4)
{
//Get Barchart Rating
var vr_from = req.responseText.substring (req.responseText.indexOf("Overall Average:"),req.responseText.length)
var vr_to = vr_from.indexOf("</B></TD></TR>")
var vr_barchart = vr_from.substring(vr_from.indexOf("&nbsp;") + 6,vr_to)

get_stockscouter_rating(i,vr_company,vr_price,vr_change,vr_barchart)
}
}
req.send(null)
}

function get_stockscouter_rating(i,vr_company,vr_price,vr_change,vr_barchart)
{
req.open("GET","http://moneycentral.msn.com/stock_quote?Symbol=US%3A" + vr_symbol_arry[i],false)
req.onreadystatechange = function()
{

if(req.readyState == 4)
{
//Get Stockscouter Rating
var vr_from = req.responseText.substring (req.responseText.indexOf("StockScouter rating:"),req.responseText.length)
var vr_to = vr_from.indexOf("<a href")
var vr_stockscouter = vr_from.substring(20,vr_to)

get_caps_rating(i,vr_company,vr_price,vr_change,vr_barchart,vr_stockscouter)
}
}
req.send(null)
}

function get_caps_rating(i,vr_company,vr_price,vr_change,vr_barchart,vr_stockscouter)
{
req.open("GET","http://www.fool.com/search/index.aspx?go=1&site=USMF&q=" + vr_symbol_arry[i] + "&source=ifltnvsnq0000001&mbbid=BoardID&mbmid=MessageID",false)
req.onreadystatechange = function()
{

if(req.readyState == 4)
{
//Get Caps Rating
var vr_from = req.responseText.substring (req.responseText.indexOf("lrg_"),req.responseText.length)
var vr_to = vr_from.indexOf(".")
var vr_caps = vr_from.substring(4,vr_to)

vr_content = vr_content + "<table border = 1 cellspacing = 0 cellpadding = 0 bordercolor = '#c2d69a'><tr align ='center'><td nowrap width = '40px'>" + i + "</td><td nowrap width = '60px'>" + vr_symbol_arry[i] + "</td><td nowrap width = '200px'>" + vr_company + "</td><td nowrap width = '60px'>" + vr_price + "</td><td nowrap width = '60px'>" + vr_change + "</td><td nowrap width = '150px'>" + vr_barchart + "</td><td nowrap width = '150px'>" + vr_stockscouter + "</td><td nowrap width = '100px'>" + vr_caps +"</td><td nowrap width = '50px'>OK</td></tr></table>"
document.getElementById("dv_content2").innerHTML = (vr_content)
}
}
req.send(null)
}

boogyman
01-14-2008, 07:12 PM
please review rule #8 on thread Posting Policies and Regulations (http://www.dynamicdrive.com/forums/showthread.php?t=24866). You will need to scroll down a little bit.