易截截图软件、单文件、免安装、纯绿色、仅160KB

Javascript简易计时器(用来记算代码的执行时间)

自己写的一个简易计时器,能记算代码的执行时间,还可以拿来测试代码的执行效率。
function Counter(){
this.start();
}
Counter.prototype.getTime = function(){
var time = new Date();
return time.getSeconds()*1000+time.getMilliseconds();
}
Counter.prototype.start = function(){
this.counting = true;
this.startTime = this.getTime();
}
Counter.prototype.stop = function(){
if(this.counting == true){
this.counting = false;
this.stopTime = this.getTime();
}
}
Counter.prototype.show = function(){
this.counting==true && this.stop();
this.time = this.stopTime-this.startTime;
document.write('执行代码花费了 '+this.time+' 毫秒<br>');
this.start();
}
使用示例如下:
var myCounter = new Counter();
for(var i=0;i<500;i++){
document.write('|');
}
myCounter.show();
var myCounter2 = new Counter();
document.write('<br>');
myCounter2.start(); //重新开始计时
for(var i=0;i<500;i++){
document.write('*');
}
myCounter2.stop(); //停止计时
for(var i=0;i<500;i++){
document.write(';');
}
myCounter2.show(); //这里显示的时间是执行第一次for循环所用的时间(就是输出一排*的那个)
在创建对象后会自动开始计时。
调用对象的start()方法将重新开始计时。
调用stop()方法会停止计时。
show()方法用来显示代码执行的时间(如果调用show()方法前没有调用过stop(),会自动调用一次)
多个计时对象之间互不影响。
因为我还是个菜鸟,希望大家对我的不足之处点评指正!


相关文档:

javascript: 正则表达式

正则表达式
RegExp(regexp, option)类实现,可以简写成/regexp/option
option:
g: global, i: ignore case
方法:string.test(regexp),
string.exec(regexp)[返回所有匹配的地方], string.serch(regexp)[正则版的indexOf()],
string.replace(regexp, str|funtion), string.split(regexp)
简单模式
元字符:( [ {
......

Javascript 类和命名空间的定义

//1.类
function Test(id)
{
     this.id=id;
     this.method=function()
         {
            //代码
         };
}
......

javascript php 之间传递 中文 避免乱码

在javascript代码中用encodeURIComponent()函数处理中文字符串,
JS代码:
<mce:script type=”text/javascript”><!--
string = encodeURIComponent(string);
location.href = index.php?keyword=’+string;
// --></mce:script>
PHP代码:
<?php
$keyword = (isset($_GET ......

JavaScript 判断浏览器类型及版本

$(document).ready(function() {
        var Sys = {};
        var ua = navigator.userAgent.toLowerCase();
        if (window.ActiveXObject)
        &nbs ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号