(func, wait, immediate)
| 1177 | // parameter. If `immediate` is passed, the argument function will be |
| 1178 | // triggered at the beginning of the sequence instead of at the end. |
| 1179 | function debounce(func, wait, immediate) { |
| 1180 | var timeout, previous, args, result, context; |
| 1181 | |
| 1182 | var later = function() { |
| 1183 | var passed = now() - previous; |
| 1184 | if (wait > passed) { |
| 1185 | timeout = setTimeout(later, wait - passed); |
| 1186 | } else { |
| 1187 | timeout = null; |
| 1188 | if (!immediate) result = func.apply(context, args); |
| 1189 | // This check is needed because `func` can recursively invoke `debounced`. |
| 1190 | if (!timeout) args = context = null; |
| 1191 | } |
| 1192 | }; |
| 1193 | |
| 1194 | var debounced = restArguments(function(_args) { |
| 1195 | context = this; |
| 1196 | args = _args; |
| 1197 | previous = now(); |
| 1198 | if (!timeout) { |
| 1199 | timeout = setTimeout(later, wait); |
| 1200 | if (immediate) result = func.apply(context, args); |
| 1201 | } |
| 1202 | return result; |
| 1203 | }); |
| 1204 | |
| 1205 | debounced.cancel = function() { |
| 1206 | clearTimeout(timeout); |
| 1207 | timeout = args = context = null; |
| 1208 | }; |
| 1209 | |
| 1210 | return debounced; |
| 1211 | } |
| 1212 | |
| 1213 | // Returns the first function passed as an argument to the second, |
| 1214 | // allowing you to adjust arguments, run code before and after, and |
nothing calls this directly
no test coverage detected
searching dependent graphs…