var Person = function(name){ this.name = name; } Person.prototype = { say: function(){ alert(this.name) return this }, delay: function(t){ t = t || 0; alert(t) } }var c = new Person(123); c.say().delay(1000)
var objPerson = function(name){ this.name = name; }objPerson.prototype = { say: function(){ //立即say alert(this.name); }, say:function(delayTime){ //延时delayTime秒say, //如不重载就改名称 delaySay即可 var t = delayTime||0; var _this=this; setTimeout(function(){alert(_this.name);},t*1000); }, /* delay: function(t){ t = t || 0; } */ }var c = new objPerson('shenzhenNBA'); c.say(); //立即say c.say(3); //延时3秒say 逻辑上看代码:say().delay() 已经say() 了, 再delay()就无意义了应该重载say函数,这样有参数就延时say,没有参数就立即say,如果不想重载就另起一个名字delaySay也可以
var Person = function(name){ this.name = name; } Person.prototype = { delay: function(t){ t = t || 0; this.t = t; return this; }, say: function(){ var that = this, t = that.t window.setTimeout(function(){ alert(that.name) },t); return this; } };var c = new Person(123); c.delay(1000).say()
简单点 var Person = function(name){ this.name = name; } Person.prototype = { say: function(t){ var that = this, t = (t)?t:0; window.setTimeout(function(){ alert(that.name); },t); } }; var c = new Person(123); c.say() //布延迟 c.say(1000) //延迟
var Person = function(name){ this.name = name; } Person.prototype = { delay: function(t){ this.t = t || 0; return this; }, say: function(){ var that = this; window.setTimeout(function(){ window.setTimeout(function(){ alert(that.name) },that.t) },0);
return this; } };var c = new Person(123); c.say().delay(1000); c.delay(1000);.say(); 都可以 别的方法没有想到
var Person = function (name) { this.name = name; } Person.prototype = { delay: function (t) { t = t || 0; if (this.h) { clearTimeout(this.h); this.h = null; this.say(this.w, t); } return this; }, say: function (word, t) { t = t || 100; this.w = word; var name = this.name; this.h = window.setTimeout(function () { alert(name + " say: " + word); }, t); return this; } }; var c = new Person(123); c.say("i love you").delay(2000);
var c=new Person(123);
setTimeOut(function(){c.say();},1000);
这个就可以在1秒后执行c.say()
已经say() 了, delay()就无意义了吧我觉得可以 delay(say)才疏学浅啊,不知道说的对不对
this.name = name;
}
Person.prototype = {
say: function(){
alert(this.name)
return this
},
delay: function(t){
t = t || 0;
alert(t)
}
}var c = new Person(123);
c.say().delay(1000)
var objPerson = function(name){
this.name = name;
}objPerson.prototype = {
say: function(){ //立即say
alert(this.name);
},
say:function(delayTime){ //延时delayTime秒say, //如不重载就改名称 delaySay即可
var t = delayTime||0;
var _this=this;
setTimeout(function(){alert(_this.name);},t*1000);
},
/*
delay: function(t){
t = t || 0;
}
*/
}var c = new objPerson('shenzhenNBA');
c.say(); //立即say
c.say(3); //延时3秒say
逻辑上看代码:say().delay()
已经say() 了, 再delay()就无意义了应该重载say函数,这样有参数就延时say,没有参数就立即say,如果不想重载就另起一个名字delaySay也可以
this.name = name;
}
Person.prototype = {
delay: function(t){
t = t || 0;
this.t = t;
return this;
},
say: function(){
var that = this,
t = that.t
window.setTimeout(function(){
alert(that.name)
},t);
return this;
}
};var c = new Person(123);
c.delay(1000).say()
var Person = function(name){
this.name = name;
}
Person.prototype = {
say: function(t){
var that = this,
t = (t)?t:0;
window.setTimeout(function(){
alert(that.name);
},t);
}
};
var c = new Person(123);
c.say() //布延迟
c.say(1000) //延迟
http://jsbin.com/isafum/3/edit#preview
var Person = function(name){
this.name = name;
}
Person.prototype = {
say: function(){
alert(this.name)
},
delay: function(time,callBack){///callBack为延时时间到时会回调函数
setTimeOut(function(){callBack();},time) ; }
}
var c = new Person(123);
c.delay(1000,c.say)///这样将在1000毫秒后执行c.say,也可以传递其他函数来延迟执行
this.name = name;
}
Person.prototype = {
delay: function(t){
this.t = t || 0;
return this;
},
say: function(){
var that = this;
window.setTimeout(function(){
window.setTimeout(function(){
alert(that.name)
},that.t)
},0);
return this;
}
};var c = new Person(123);
c.say().delay(1000);
c.delay(1000);.say();
都可以
别的方法没有想到
var Person = function (name) {
this.name = name;
}
Person.prototype = {
delay: function (t) {
t = t || 0;
if (this.h) {
clearTimeout(this.h);
this.h = null;
this.say(this.w, t);
}
return this;
},
say: function (word, t) {
t = t || 100;
this.w = word;
var name = this.name;
this.h = window.setTimeout(function () {
alert(name + " say: " + word);
}, t);
return this;
}
}; var c = new Person(123);
c.say("i love you").delay(2000);
(function(){/**
* @method proxy(Function fn, Object scope, Array args)
* 创建代理函数,传入一个Function对象,返回该函数的代理函数,并可指定函数内的this 作用域,和函数被调用时的参数
*
* <pre>
* 1、fn - 被代理的函数
* 2、scope - 被代理函数内部this的作用域
* 3、args - 参数数组
* </pre>
*
* @return 代理函数
*/
var proxy = function(fn, scope, args) {
arguments.length > 2 && !Fan.isArray(args) && (args = [args]);
return function() {
var ret;
Fan.isFunction(fn) && (ret = fn.apply(scope || this, args || arguments));
return ret;
};
}; /**
* @staticMethod defer(Function fn, int lazyTime, Object scope, Array args)
* 延迟执行函数
*
* <pre>
* 1、fn - 被延迟执行的函数
* 2、lazyTime - 延迟时间
* 3、scope - 延迟函数中的this作用域,可选
* 4、args - 参数数组,可选
* </pre>
*
* @return setTimeout标示
*/
var defer = function(fn, lazyTime, scope, args) {
if (Fan.isFunction(fn)) {
var proxyFn = (null != scope || null != args) ? proxy(fn, scope, args) : fn;
return setTimeout(proxyFn, lazyTime);
}
};// 扩展到Function原型上
Function.prototype.proxy = proxy;
Function.prototype.defer = defer;})();
使用时只要:var c = new Person(123);
c.say.defer(1000)
* @method proxy(Function fn, Object scope, Array args)
* 创建代理函数,传入一个Function对象,返回该函数的代理函数,并可指定函数内的this 作用域,和函数被调用时的参数
*
* <pre>
* 1、fn - 被代理的函数
* 2、scope - 被代理函数内部this的作用域
* 3、args - 参数数组
* </pre>
*
* @return 代理函数
*/
var proxy = function(fn, scope, args) {
arguments.length > 2 && !('[object Array]' == Object.prototype.toString.apply(args)
&& null != args.length) && (args = [args]);
return function() {
var ret;
(typeof fn == 'function' || fn instanceof Function) && (ret = fn.apply(scope || this, args || arguments));
return ret;
};
}; /**
* @staticMethod defer(Function fn, int lazyTime, Object scope, Array args)
* 延迟执行函数
*
* <pre>
* 1、fn - 被延迟执行的函数
* 2、lazyTime - 延迟时间
* 3、scope - 延迟函数中的this作用域,可选
* 4、args - 参数数组,可选
* </pre>
*
* @return setTimeout标示
*/
var defer = function(fn, lazyTime, scope, args) {
if ((typeof fn == 'function' || fn instanceof Function)) {
var proxyFn = (null != scope || null != args) ? proxy(fn, scope, args) : fn;
return setTimeout(proxyFn, lazyTime);
}
};// 扩展到Function原型上
Function.prototype.proxy = proxy;
Function.prototype.defer = defer;})();
Function.prototype.delay = function(delay, bind){
var thiz = this;
return function(){
setTimeout(function(){
thiz.call(bind || window);
}, delay);
};
}
var obj = {
name:'liu',
say:function(){
alert(this.name);
},
delaySay:function(){
var delayFunc = this.say.delay(1000, this);
delayFunc();
}
}
obj.delaySay();
这样可以不污染原有的say,感觉健壮性上好点。