當前位置:軟件學堂 > 資訊首頁 > 網絡編程 > 編程其他 > 利用JS代碼檢測站點的鏈接速度

利用JS代碼檢測站點的鏈接速度

2012/10/29 15:59:40作者:佚名來源:網絡

移動端

【實例名稱】

利用JS代碼檢測站點的鏈接速度

【實例描述】

判斷一個站點的好壞,界面和速度都是關鍵因素。本例學習如何測試一個網站的鏈接速度。

【實例代碼】

<html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>標題頁-學無憂(www.yzddtk.cn)</title> </head> <body> <script> indexArr=0                      //連接網站的時間 setInterval("indexArr++",100)   //設置循環的定時器 b=1 var autourl=new Array()         //設置一個鏈接數組 autourl[1]="mail.163.com" autourl[2]="www.263.net" autourl[3]="www.sina.com.cn" autourl[4]="www.baidu.com" autourl[5]="www.cctv.com"

function writeBtn(){     document.write("<form name=autof>") //輸出一個提交窗體     for(var i=1;i<autourl.length;i++)     document.write("<input type=text name=txt"+i+" size=12 value=測試中……> =》<input type=text name=url"+i+" size=40> =》<input type=button value=GO onclick=window.open(this.form.url"+i+".value)><br>")     document.write("<input type=submit value=刷新></form>") //輸出一個提交按鈕 } writeBtn() function auto(url){     document.forms[0]["url"+b].value=url     if(indexArr>200)     {document.forms[0]["txt"+b].value="鏈接超時"}               //如果超時,則提示此信息     else     {document.forms[0]["txt"+b].value="時間"+indexArr/100+"毫秒"}  //顯示鏈接站點的時間     b++ } function run(){     for(var i=1;i<autourl.length;i++)                           //循環測試鏈接站點的時間     document.write("<img src=http://"+autourl[i]+"/"+Math.random() +" width=1 height=1 onerror=auto('http://"+autourl[i]+"')>") } run() </script> </body> </html>

【運行效果】

運行效果

【難點剖析】

本例的目的是測試網站的鏈接速度,其中使用了一個定時器,每隔100毫秒計時一次(“indexArr’’變量自增),當站點連接完畢后,獲取“indexArr”變量的值再除以100,便是連接站點需要的毫秒值。

【源碼下載】

如果你不愿復制代碼及提高代碼準確性,你可以點擊:檢測站點的鏈接速度 進行本實例源碼下載 

欧美一级一极性活片免费观看,欧美一级艳片欧美精品,欧美一级性爱大片,欧美一级囗交视频 视频,欧美一级特黄录像视频