{"action":"opened","issue":{"url":"https://api.github.com/repos/tianlin901212/f2e-questions/issues/14","repository_url":"https://api.github.com/repos/tianlin901212/f2e-questions","labels_url":"https://api.github.com/repos/tianlin901212/f2e-questions/issues/14/labels{/name}","comments_url":"https://api.github.com/repos/tianlin901212/f2e-questions/issues/14/comments","events_url":"https://api.github.com/repos/tianlin901212/f2e-questions/issues/14/events","html_url":"https://github.com/tianlin901212/f2e-questions/issues/14","id":395473618,"node_id":"MDU6SXNzdWUzOTU0NzM2MTg=","number":14,"title":"防抖节流","user":{"login":"tianlin901212","id":6746461,"node_id":"MDQ6VXNlcjY3NDY0NjE=","avatar_url":"https://avatars0.githubusercontent.com/u/6746461?v=4","gravatar_id":"","url":"https://api.github.com/users/tianlin901212","html_url":"https://github.com/tianlin901212","followers_url":"https://api.github.com/users/tianlin901212/followers","following_url":"https://api.github.com/users/tianlin901212/following{/other_user}","gists_url":"https://api.github.com/users/tianlin901212/gists{/gist_id}","starred_url":"https://api.github.com/users/tianlin901212/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tianlin901212/subscriptions","organizations_url":"https://api.github.com/users/tianlin901212/orgs","repos_url":"https://api.github.com/users/tianlin901212/repos","events_url":"https://api.github.com/users/tianlin901212/events{/privacy}","received_events_url":"https://api.github.com/users/tianlin901212/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2019-01-03T06:59:14Z","updated_at":"2019-01-03T06:59:14Z","closed_at":null,"author_association":"OWNER","body":"```\r\n防抖\r\n/**\r\n* @param fn {Function}   实际要执行的函数\r\n* @param delay {Number}  延迟时间，也就是阈值，单位是毫秒（ms）\r\n* @return {Function}     返回一个“去弹跳”了的函数\r\n*/\r\nexport const debounce = function(fn, delay) {\r\n    var timer; // 定时器，用来 setTimeout\r\n    // 返回一个函数，这个函数会在一个时间区间结束后的 delay 毫秒时执行 fn 函数\r\n    return function () {\r\n      // 保存函数调用时的上下文和参数，传递给 fn\r\n      var context = this\r\n      var args = arguments\r\n  \r\n      clearTimeout(timer) // 每次这个返回的函数被调用，就清除定时器，以保证不执行 fn\r\n      // 当返回的函数被最后一次调用后（也就是用户停止了某个连续的操作），\r\n      // 再过 delay 毫秒就执行 fn\r\n      timer = setTimeout(function () {\r\n        fn.apply(context, args)\r\n      }, delay)\r\n    }\r\n  }\r\n\r\n节流\r\n/**\r\n* @param fn {Function}   实际要执行的函数\r\n* @param delay {Number}  执行间隔，单位是毫秒（ms）\r\n* @return {Function}     返回一个“节流”函数\r\n*/\r\nexport const throttle = function(fn, threshhold) {\r\n    var last; // 记录上次执行的时间\r\n    var timer; // 定时器\r\n    threshhold || (threshhold = 250) // 默认间隔为 250ms\r\n  \r\n    // 返回的函数，每过 threshhold 毫秒就执行一次 fn 函数\r\n    return function () {\r\n      // 保存函数调用时的上下文和参数，传递给 fn\r\n      var context = this\r\n      var args = arguments\r\n  \r\n      var now = +new Date()\r\n      // 如果距离上次执行 fn 函数的时间小于 threshhold，那么就放弃\r\n      // 执行 fn，并重新计时\r\n      if (last && now < last + threshhold) {\r\n        clearTimeout(timer)\r\n  \r\n        // 保证在当前时间区间结束后，再执行一次 fn\r\n        timer = setTimeout(function () {\r\n          last = now\r\n          fn.apply(context, args)\r\n        }, threshhold)\r\n  \r\n      // 在时间区间的最开始和到达指定间隔的时候执行一次 fn\r\n      } else {\r\n        last = now\r\n        fn.apply(context, args)\r\n      }\r\n    }\r\n  }\r\n```"}}
{"actor":{"display_login":"tianlin901212"}}