<!DOCTYPE html><html><head><meta charset="utf-8"><title>函数节流 Function throttling</title></head> <body>函数节流 Function throttling ......<script> //函数节流 let throttle= function(fnToBeExecuted,delay){ let timer; let firstTime = true; return function(){ let closureThis = this; let closureArgs = arguments; if(timer){ return false; } if(firstTime){ fnToBeExecuted.apply(closureThis,closureArgs); firstTime = false; } timer = setTimeout(function(){ clearTimeout(timer); timer = null; fnToBeExecuted.apply(closureThis,closureArgs); },(delay || 500)); } } window.onresize = throttle(function(){console.log('throttle')},3000); </script></body></html>
// 通用的单例模式 let getSingleton = function(fn)(){ let instance; return function(){ return instance || (instance = fn.apply(this,arguments)); } }; getSingleton(function(){//createSomethingThenReturn})