js delay函数怎么实现
var Person = function(name){ this.name = name; } Person.prototype = { say: function(){ alert(this.name) }, delay: function(t){ t = t || 0; } } var c = new Person(123); c.say().delay(1000)
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(); //立即sayc.say(3); //延时3秒say
[解决办法]
7楼的代码已经可以了,如果楼主一定要保留一个delay函数的话可以这样写:
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,也可以传递其他函数来延迟执行
[解决办法]
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);
[解决办法]
(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;})();
[解决办法]
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();