函数节流
2015-08-04 22:07
357 查看
http://www.alloyteam.com/2012/11/javascript-throttle/
1. 必须用context传递this,否则setTimeout中this为window对象
1. 必须用context传递this,否则setTimeout中this为window对象
function fn(){} var throttle=function(fn,delay) { var timer=null; return function() { var context=this,args=arguments; clearTimeout(timer); timer=setTimeout(function() { fn.apply(context,args); console.log(context);//input console.log(this);//window },delay); }; }; var btn=document.getElementById('my-btn'); btn.addEventListener('click',throttle(fn,50),false);
相关文章推荐
- !HDU 4345 置换群的环长有多少种-dp
- hdu_5551_多校联赛5_1009
- cause:Permission Denial: receiving Intent act=android.provider.Telephony.SMS_RECEIVED
- 多线程1
- Count the Trees 典型卡特兰数
- 【Objective-C编程】import、include及@Class的孽缘
- VistualStudio 2005 设置PowerBuilder12.5 PBNI编绎命令行。
- 浅谈WebView
- Docker 容器日志的那些事儿
- 【Mac版】安装 Android Studio 及配置环境变量步骤
- 没做过编译器就是被人欺——从一道变态的i++题猜编译器的行为(表达式从左往右扫描,同一变量相互影响)
- 关于foreach的一个BUG
- leetcode面试准备:Count Complete Tree Nodes
- Struts拦截器拦截未登录的操作
- sudo 问题
- 黑马程序员--文件字符输入输出函数的应用
- day04_循环结构_20150804
- 获取鼠标XY坐标
- 例子
- FileInputStream与FileOutputStream练习题 -------------------图片拷贝