編程(Programming)是編定程序的中文簡(jiǎn)稱,就是讓計(jì)算機(jī)代碼解決某個(gè)問(wèn)題,對(duì)某個(gè)計(jì)算體系規(guī)定一定的運(yùn)算方式,使計(jì)算體系按照該計(jì)算方式運(yùn)行,并最終得到相應(yīng)結(jié)果的過(guò)程。為了使計(jì)算機(jī)能夠理解(understand)人的意圖,人類就必須將需解決的問(wèn)題的思路、方法和手段通過(guò)計(jì)算機(jī)能夠理解的形式告訴計(jì)算機(jī),使得計(jì)算機(jī)能夠根據(jù)人的指令一步一步去工作,完成某種特定的任務(wù)。這種人和計(jì)算體系之間交流的過(guò)程就是編程。 【實(shí)例名稱】 利用JS代碼檢測(cè)站點(diǎn)的鏈接速度 【實(shí)例描述】 判斷一個(gè)站點(diǎn)的好壞,界面和速度都是關(guān)鍵因素。本例學(xué)習(xí)如何測(cè)試一個(gè)網(wǎng)站的鏈接速度。 【實(shí)例代碼】 <html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>標(biāo)題頁(yè)-本站(www.xue51.com)</title>
</head>
<body>
<script>
indexArr=0 //連接網(wǎng)站的時(shí)間
setInterval("indexArr++",100) //設(shè)置循環(huán)的定時(shí)器
b=1
var autourl=new Array() //設(shè)置一個(gè)鏈接數(shù)組
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>")
//輸出一個(gè)提交窗體
for(var i=1;i<autourl.length;i++)
document.write("<input type=text
name=txt"+i+" size=12 value=測(cè)試中……> =》<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>")
//輸出一個(gè)提交按鈕
}
writeBtn()
function auto(url){
document.forms[0]["url"+b].value=url
if(indexArr>200)
{document.forms[0]["txt"+b].value="鏈接超時(shí)"}
//如果超時(shí),則提示此信息
else
{document.forms[0]["txt"+b].value="時(shí)間"+indexArr/100+"毫秒"}
//顯示鏈接站點(diǎn)的時(shí)間
b++
}
function run(){
for(var i=1;i<autourl.length;i++)
//循環(huán)測(cè)試鏈接站點(diǎn)的時(shí)間
document.write("<img src=http://"+autourl[i]+"/"+Math.random()
+" width=1 height=1 onerror=auto('http://"+autourl[i]+"')>")
}
run()
</script>
</body>
</html>
【運(yùn)行效果】 
【難點(diǎn)剖析】 本例的目的是測(cè)試網(wǎng)站的鏈接速度,其中使用了一個(gè)定時(shí)器,每隔100毫秒計(jì)時(shí)一次(“indexArr’’變量自增),當(dāng)站點(diǎn)連接完畢后,獲取“indexArr”變量的值再除以100,便是連接站點(diǎn)需要的毫秒值。 【源碼下載】 如果你不愿復(fù)制代碼及提高代碼準(zhǔn)確性,你可以點(diǎn)擊:檢測(cè)站點(diǎn)的鏈接速度 進(jìn)行本實(shí)例源碼下載
使用編程語(yǔ)言寫的程序,由于每條指令都對(duì)應(yīng)計(jì)算機(jī)一個(gè)特定的基本動(dòng)作,所以程序占用內(nèi)存少、執(zhí)行效率高。 |