惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 最新话题
C
Cyber Attacks, Cyber Crime and Cyber Security
G
GRAHAM CLULEY
T
Tenable Blog
T
Threatpost
C
CXSECURITY Database RSS Feed - CXSecurity.com
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
Security Latest
Security Latest
P
Privacy & Cybersecurity Law Blog
Google Online Security Blog
Google Online Security Blog
SecWiki News
SecWiki News
P
Palo Alto Networks Blog
TaoSecurity Blog
TaoSecurity Blog
Webroot Blog
Webroot Blog
Spread Privacy
Spread Privacy
O
OpenAI News
The Last Watchdog
The Last Watchdog
P
Proofpoint News Feed
C
Check Point Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
人人都是产品经理
人人都是产品经理
S
Security @ Cisco Blogs
Scott Helme
Scott Helme
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
月光博客
月光博客
S
Securelist
酷 壳 – CoolShell
酷 壳 – CoolShell
V
V2EX
T
Troy Hunt's Blog
W
WeLiveSecurity
GbyAI
GbyAI
N
News | PayPal Newsroom
Y
Y Combinator Blog
C
Cisco Blogs
H
Help Net Security
The GitHub Blog
The GitHub Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 【当耐特】
Jina AI
Jina AI
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
云风的 BLOG
云风的 BLOG
小众软件
小众软件
N
News and Events Feed by Topic

博客园 - 骨月枫🍁

使用vLLM部署Qwen/Qwen3.5-35B-A3B-FP8并且在DIFY中调用 记录个人数组、字符串自己常忘记的方法,以及ES常用处理方式 解决webpack vue 项目打包生成的文件,资源文件均404问题 js 复制粘贴功能记录 记录下工作中使用的pdf.js 记录下jplayer的简单demo 用ajax与fetch调用阿里云免费接口 简要谈谈javascript bind 方法 h5启动原生APP总结 mac上安装mongoDb以及简单使用 mac快捷键整理以及node的基本使用 html5视频video积累 整理下PC和移动获取点击、移动坐标的代码和坑 html5 实现简单的上传 canvas基础学习(四) canvas基础学习(三) canvas基础学习(二) canvas基础学习(一) 移动端使用百度分享代码
js 函数节流
骨月枫🍁 · 2017-09-08 · via 博客园 - 骨月枫🍁

  在JS中,函数的调用大多数都是由用户主动调用触发,但是在有的事件中,比如mousemove、window.onresize、touchmove中,函数的调用次数会非常频繁,从而消耗浏览器大量的内存空间,造成浏览器卡顿甚至假死的问题。所以函数节流的目的就是减少函数在这些事件中的调用次数,从不可控制到可控。

  函数节流的实现方式有很多中,通用的原理就是使用setTimeout函数,延迟执行事件处理函数,在没有执行这个函数前,再次调用它时都忽略,具体实现方式如下:

/*
 * 节流函数
 * fn 事件中实际需要调用的函数
 * interval    函数最短隔多长时间调用
 * */
var throttle = function(fn , interval){
    var _self = fn ,     timer ,        //定时器
        isFirst = true;        //是否是第一次调用
    return function(){
        var args = arguments ,
            _this = this;
        if(isFirst){            //如果是第一次执行,不需要走定时器
            _self.apply(_this , args);
            return isFirst = false;
        }
        /*从第二次执行就要开始走定时器了*/
        if(timer)    return;            //如果定时器内的函数还未执行return
        timer = setTimeout(function(){        //定时函数
            clearTimeout(timer);
            timer = null;
            _self.apply(_this , args);
        } , interval || 100)
    }
}
//测试
window.onresize = throttle(function(){
    console.log(222);
} , 500);

  然后可以对比下不使用throttle方法,会发现打印的频率明显的降低

搞定!!!