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(),会自动调用一次)
多个计时对象之间互不影响。
因为我还是个菜鸟,希望大家对我的不足之处点评指正!
相关文档:
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 ......
正则表达式
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.无提示刷新网页
大家有没有发现,有些网页,刷新的时候,会弹出一个提示窗口,点“确定”才会刷新。
而有的页面不会提示,不弹出提示窗口,直接就刷新了.
如果页面没有form,则不会弹出提示窗口。如果页面有form表单,
a)< fo ......
以下分别是用递归和两种循环对斐波那契数列的简单实现。(结果仅供参考)
递归的模式:
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 ......