本文實(shí)例講述了Javascript實(shí)現(xiàn)的StopWatch功能。分享給大家供大家參考,具體如下:
創(chuàng)新互聯(lián)建站10多年企業(yè)網(wǎng)站建設(shè)服務(wù);為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計(jì)及高端網(wǎng)站定制服務(wù),企業(yè)網(wǎng)站建設(shè)及推廣,對(duì)雨棚定制等多個(gè)領(lǐng)域擁有豐富的營(yíng)銷推廣經(jīng)驗(yàn)的網(wǎng)站建設(shè)公司。
有時(shí)會(huì)需要js來寫一些函數(shù)進(jìn)行測(cè)試,如果需要測(cè)試執(zhí)行時(shí)間,可能需要一個(gè)stopwatch:
StopWatch類:
function stopWatch() { } stopWatch.prototype.Start = function () { this.startD = new Date(); return this; }; stopWatch.prototype.Stop = function () { this.startD = new Date(); return this; }; stopWatch.prototype.Seconds = function () { return Math.abs((new Date() - this.startD) / 1000); };
用法示例(測(cè)試斐波那契數(shù)列):
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>StopWatch</title> </head> <body> <script > function stopWatch() { } stopWatch.prototype.Start = function () { this.startD = new Date(); return this; }; stopWatch.prototype.Stop = function () { this.startD = new Date(); return this; }; stopWatch.prototype.Seconds = function () { return Math.abs((new Date() - this.startD) / 1000); }; var sw = new stopWatch().Start(); (function f(n){return n == 1 || n == 2 ? 1 : f(n-1)+f(n-2);})(45); alert(sw.Seconds()); </script> </body> </html>
運(yùn)行效果圖如下:
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript字符與字符串操作技巧總結(jié)》、《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》、《JavaScript中json操作技巧總結(jié)》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》及《JavaScript遍歷算法與技巧總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
本文題目:Javascript實(shí)現(xiàn)的StopWatch功能示例
新聞來源:http://www.chinadenli.net/article28/gcipcp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、建站公司、響應(yīng)式網(wǎng)站、定制開發(fā)、標(biāo)簽優(yōu)化、網(wǎng)頁設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
營(yíng)銷型網(wǎng)站建設(shè)知識(shí)