<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>节流</title> </head> <body> <input type="text" /> <input type="submit" id="input" /> <script type="text/javascript"> let btn = document.getElementById('input') btn.addEventListener('click',throttle(submit,2000),false) function submit(params){ console.log(params) console.log(this) } function throttle(fn,delay){ let begin = 0 ; return function(e){ let cur = new Date().getTime(); console.log(cur-begin) if(cur - begin > delay){ fn.call(this,arguments) begin = cur; } } } </script> </body> </html>
效果图