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

解决方案 »

  1.   

    不是你那样写吧
    var c=new Person(123);
    setTimeOut(function(){c.say();},1000);
    这个就可以在1秒后执行c.say()
      

  2.   

    代码:say().delay()
    已经say() 了, delay()就无意义了吧我觉得可以 delay(say)才疏学浅啊,不知道说的对不对
      

  3.   

     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)
      

  4.   


    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也可以
      

  5.   

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

  6.   

    简单点
     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) //延迟
      

  7.   

    你的delay要不要适用于所有function?
    http://jsbin.com/isafum/3/edit#preview
      

  8.   

    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,也可以传递其他函数来延迟执行
      

  9.   

    师兄,c.say().delay(600)这样就不行了,那类似 一些 框架,可以***.animate({...}).delay() 这样是怎么实现的?
      

  10.   

    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();
    都可以
    别的方法没有想到
      

  11.   


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

  12.   


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

  13.   


    使用时只要:var c = new Person(123);
    c.say.defer(1000)
      

  14.   

    代码是摘自Fan框架需要稍微修改一下:(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 && !('[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;})();
      

  15.   


    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,感觉健壮性上好点。