股票代码使网站负载非常长

时间:2013-12-12 19:55:05

标签: javascript php joomla

我需要建立一个带有spechips股票的股票代码,经过一段很长的搜索后我发现Simple PHP Stock Ticker and Javascript Marquee for valid XHTML经过一些调整和正确的.htaccess文件后,我设法让它工作,当它本身就是作为一个单独的html文件它或多或少都没关系但是当我将它添加到joomla时,带有自动收报机的页面需要极长的加载,我还没有添加“stockcache”目录,所以它实际上并没有加载任何东西,但它仍然是需要很长时间才能完成。

这是代码(独立的html文件)

<!DOCTYPE html>
<head>
<meta charset="utf-8">

<title>stockticker</title>
<!--    <link rel="stylesheet" href="stockticker.css"> -->

<style type="text/css"> 
#marqueeborder {
color: #cccccc;
font-family:"Verdana", Monaco, monospace;
position:relative;
height:20px; 
overflow:hidden;
font-weight:bold;
font-size: 0.7em;
}
#marqueecontent {
position:absolute;
left:0px;
line-height:20px;
white-space:nowrap;
}
.stockbox {
margin:0 10px;
color: #000044;
}
.stockboxname {
color: #359ad2;
}

</style>

<script type="text/javascript">
    // Original script by Walter Heitman Jr, first published on     http://techblog.shanock.com

    // Set an initial scroll speed. This equates to the number of pixels shifted per         tick
var scrollspeed=3;
var pxptick=scrollspeed;
function startmarquee(){
    // Make a shortcut referencing our div with the content we want to scroll
    marqueediv=document.getElementById("marqueecontent");
    // Get the total width of our available scroll area
    marqueewidth=document.getElementById("marqueeborder").offsetWidth;
    // Get the width of the content we want to scroll
    contentwidth=marqueediv.offsetWidth;
    // Start the ticker at 50 milliseconds per tick, adjust this to suit your      preferences
    // Be warned, setting this lower has heavy impact on client-side CPU usage.     Be gentle.
    setInterval("scrollmarquee()",50);
    }
function scrollmarquee(){
    // Check position of the div, then shift it left by the set amount of     pixels.
        if (parseInt(marqueediv.style.left)>(contentwidth*(-1)))
        marqueediv.style.left=parseInt(marqueediv.style.left)-pxptick+"px";
        // If it's at the end, move it back to the right.
    else
            marqueediv.style.left=parseInt(marqueewidth)+"px";
    }
    window.onload=startmarquee;
</script>

</head>


<body>


<div id="marqueeborder" onmouseover="pxptick=0" onmouseout="pxptick=scrollspeed">
<div id="marqueecontent">

<?

    // Original script by Walter Heitman Jr, first published on     http://techblog.shanock.com

    // List your stocks here, separated by commas, no spaces, in the order you want them     displayed:
    $stocks = "<!--stocks--    >KO,FB,AAPL,JPM,MSFT,GOOG,TWTR,VOD,SCGLF,VXX,SBRCY,SSNLF,IBN,WEBNF,<!--comodeties--    >GCV14.CMX<!--gold-->,SIZ13.CMX<!--silver-->,PLF14.NYM<!--platinum-->,CZ13.CBT<!--corn--    >,KCZ13.NYB<!--cofee-->,SBH14.NYB<!--sugar-->";

    // Function to copy a stock quote CSV from Yahoo to the local cache. CSV contains     symbol, price, and change
    function upsfile($stock) { copy("http://finance.yahoo.com/d/quotes.csv?    s=$stock&f=sl1c1&e=.csv","stockcache/".$stock.".csv"); }

    foreach ( explode(",", $stocks) as $stock ) {

        // Where the stock quote info file should be...
    $local_file = "stockcache/".$stock.".csv";

    // ...if it exists. If not, download it.
    if (!file_exists($local_file)) { upsfile($stock); }
    // Else,If it's out-of-date by 15 mins (900 seconds) or more, update it.
    elseif (filemtime($local_file) <= (time() - 900)) { upsfile($stock); }

    // Open the file, load our values into an array...
    $local_file = fopen ("stockcache/".$stock.".csv","r");
    $stock_info = fgetcsv ($local_file, 1000, ",");

    // ...format, and output them. I made the symbols into links to Yahoo's stock pages.
    echo "<span class=\"stockbox\"><span class=\"stockboxname\">".$stock_info[0]."</span> ".sprintf("%.2f",$stock_info[1])." <span style=\"";
    // Green prices for up, red for down
    if ($stock_info[2]>=0) { echo "color: #009900;\">&uarr;";   }
    elseif ($stock_info[2]<0) { echo "color: #ff0000;\">&darr;"; }
    echo sprintf("%.2f",abs($stock_info[2]))."</span></span>\n";
    // Done!
    fclose($local_file); 
}

?>

<!--
<span class="stockbox" style="font-size:0.6em">Quotes from <a     href="http://finance.yahoo.com/">Yahoo Finance</a></span>
-->
</div>
</div>

</body>

我把脚本放在header.php和文章里面的php中

1 个答案:

答案 0 :(得分:1)

这条线有点混乱:

$stocks = "<!--stocks--    >KO,FB,AAPL,JPM,MSFT,GOOG,TWTR,VOD,SCGLF,VXX,SBRCY,SSNLF,IBN,WEBNF,<!--comodeties--    >GCV14.CMX<!--gold-->,SIZ13.CMX<!--silver-->,PLF14.NYM<!--platinum-->,CZ13.CBT<!--corn--    >,KCZ13.NYB<!--cofee-->,SBH14.NYB<!--sugar-->";

您需要提取评论并将其更改为:

$stocks = "KO,FB,AAPL,JPM,MSFT,GOOG,TWTR,VOD,SCGLF,VXX,SBRCY,SSNLF,IBN,WEBNF,GCV14.CMX,SIZ13.CMX,PLF14.NYM,CZ13.CBT,KCZ13.NYB,SBH14.NYB";

否则您的代码将尝试拉取,例如:

"<!--stocks--    >KO" 

作为雅虎财经的第一个股票报价,同样引起金,银,铂,玉米,咖啡和糖报价拉动的问题。 PHP没有

<!-- 

样式引用像HTML一样。它使用/ * * /样式引号。

完成后,它可能会更快,因为雅虎不必花费太多时间来弄清楚你想要的东西。