易截截图软件、单文件、免安装、纯绿色、仅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实现页面无刷新更新数据

程序设计中会经常碰到一种情况,就是事先无法得知用户会需要哪些数据,必须根据用户选择后再从服务器
重新提取数据后反馈给用户。比如一简单的情况,用户选择省份以后,我们立即会在市里边将这个省的所有
市重新显示出来。这种情况一般需要将整个页面刷新后才可以重新读取,但这样不仅效率不高外,也显得不
太优雅。其实 ......

javascript实用的N个小例子

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://gzycm520.blog.51cto.com/175220/36822
· 事件源对象 event.srcElement.tagName event.srcElement.type
· 捕获释放 event.srcElement.setCapture();  ......

javascript: The Browser Object Model(BOM)

window
window对象是浏览器或者框架自身.top总是浏览器,parent是父框架,self表示自己.
window通常可以省略.
窗口操作: moveBy(dx, dy), moveTo(x, y),
resizeBy(dw, dh), resizeTo(w, h).
导航: window.open(url, frame
name, attribute). attribute可以是left, top, height, width, resizable,
scrollable, too ......

介绍怎样解决JavaScript页面刷新与弹出窗口的问题。

 介绍怎样解决JavaScript页面刷新与弹出窗口的问题。
  1.无提示刷新网页
  大家有没有发现,有些网页,刷新的时候,会弹出一个提示窗口,点“确定”才会刷新。
  而有的页面不会提示,不弹出提示窗口,直接就刷新了.
  如果页面没有form,则不会弹出提示窗口。如果页面有form表单,
  a)< fo ......

Javascript递归与各种循环执行效率的比较

以下分别是用递归和两种循环对斐波那契数列的简单实现。(结果仅供参考)
递归的模式:
function Fibonacci(num){
if(num <= 2){
return 1;
}else{
return Fibonacci(num - 1) + Fibonacci(num - 2)
}
}
var counter1 = new Counter();
Fibonacci(30);
counter1.show()
//Firefo ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号