日期:2014-05-16  浏览次数:20406 次

js delay函数怎么实现
JScript code

            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)




以上结构,怎么实现 延时 执行say

------解决方案--------------------
JScript code
 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)

------解决方案--------------------
JScript code

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

------解决方案--------------------
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();
都可以
别的方法没有想到。。。
------解决方案--------------------
JScript code

    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);

------解决方案--------------------
JScript code

(function(){

/**
         * @method proxy(Function fn, Object scope, Array args)
         * 创建代理函数,传入一个Function对象,返回该函数的代理函数,并可指定函数内的this 作用域,和函数被调用时的参数
         * 
         * <pre>
         * 1、fn - 被代理的函数 
         * 2、scope - 被代理函