如何只执行一次函数

时间:2014-02-03 21:22:27

标签: javascript html

我有一个函数作为更大的javascript文件的一部分,它执行基于setInterval()的函数。现在虽然它使用的是间隔,但我确实希望它只执行一次,但是基于它每隔20执行一次的代码。

TINY.height=function(){
    return{
        set:function(e,h,s,d){
            e=typeof e=='object'?e:$(e); var oh=e.offsetHeight, ho=e.style.height||TINY.style.val(e,'height');
            ho=oh-parseInt(ho); var hd=oh-ho>h?-1:1;
                    clearInterval(e.si);
                    e.si=setInterval(function(){TINY.height.tw(e,h,ho,hd,s)},20)
        },
        tw:function(e,h,ho,hd,s){
            var oh=e.offsetHeight-ho;
            if(oh==h){clearInterval(e.si)}else{if(oh!=h){e.style.height=oh+(Math.ceil(Math.abs(h-oh)/s)*hd)+'px'}}
        }
    }
}();

我修改为只执行一次,然后清除间隔?

1 个答案:

答案 0 :(得分:4)

这正是setTimout()所做的。

相关问题