function randArr(myArr:Array):Array {
var myArrL:Number = myArr.length;
var tempArr;
for (i=0; i<myArrL; i++) {
a = Math.floor(Math.random()*myArrL);
b = Math.floor(Math.random()*myArrL);
tempArr = myArr[a];
myArr[a] = myArr[b];
myArr[b] = tempArr;
}
return myArr;
}
ASP版本,这里包含有个列子,是从数据库读取全部记录然后随机排列输出前n个,可以用来做到每次访问都显示不同的新闻、图片、广告等等
function randArr(arr)
dim l:l=ubound(arr)
dim tempStr, a, b
for i=0 to l
Randomize timer
a = int(rnd()*l)
b = int(rnd()*l)
tempStr = arr(a)
arr(a) = arr(b)
arr(b) = tempStr
next
randArr = arr
end function
function buildIndexAW(db, n)
set rs = server.CreateObject("adodb.recordset")
sql = "select * from works where aw=true order by w_order"
rs.open sql, conn(db), 1, 1
if not rs.eof then
dim al:al = rs.recordcount
dim j:j = 0
dim rsArr()
redim rsArr(al)
do while not rs.eof
bgimg = rs("picsml")
if bgimg="" or isnull(bgimg) then bgimg="images/thumb-sml.gif"
if isFileExists(bgimg)=false then bgimg="images/thumb-sml.gif"
id = rs("w_id")
title = TransferHTML(rs("title")) & " - " & rs("aw_words")
rsArr(j) = array(bgimg, id, title)
rs.movenext
j = j+1
loop
response.Write("<ul>")
myrsArr = randArr(rsArr)
if n>al+1 then n=al
for i=0 to n-1
response.Write("<li><a href=""works-view.asp?i=" & myrsArr(i)(1) & """ title=""" & myrsArr(i)(2) & """ style=""background-image: url(" & myrsArr(i)(0) & ");"" target=""_blank"">" & myrsArr(i)(2) & "</a></li>")
next
response.Write("</ul>")
else
response.Write("资料整理中……")
end if
rs.close
set rs = nothing
end function
另,javascript版本跟php版本类似flash的actionscript的写法,但要注意变量声明方式
转载请注明出处,有问题请留言