未加星标

Timing Controls - Part 2

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

In aprevious post, we looked at some techniques to control the timings of functions (callbacks), triggered when specific events are fired. The techniques are:

Debounce Immediate Throttle

In this post, we will see how those techniques can be implemented as a generic API.

We will work with the Debounce implementation:

var delta = 1000; var timeoutID = null; function foo() { console.log('bar'); } function debouncedFoo() { clearTimeout(timeoutID); timeoutID = setTimeout(function() { foo(); }, delta); }; window.onkeydown = debouncedFoo;

There are few problems with this implementation.

1. Passing arguments

First of all, what if foo expects some arguments? We need to provide those arguments on call. This problem can be resolved by adding parameters to debouncedFoo .

function foo(a, b, c) { // ... } function debouncedFoo(x, y, z) { // ... foo(x, y, z); // ... }; 2. Functions with unknown arity

Arity is the number of arguments the function takes. If we know how many arguments foo requires, the above works just fine. In case of unknown arity function, we can use the arguments object .

We will use .apply method because it allows function execution with the arguments being passed as an array or array-like object (ex: arguments ).

function foo(a, b, c) { // ... } function debouncedFoo(arguments) { var args = arguments; // ... foo.apply(null, args); // ... }; 3. Multiple debounced functions

Now, let’s move on to the next problem - production. How many debounced functions do you need? And are you going to hardcode them all?

A better approach is to use Higher-order functions. There are a tons of resources on HOFs, and I suggest to have a good read on the topic. In the simplest of terms, HOFs take in a function and return a function.

var delta = 1000; function log(e) { console.log(e); } function debounce(fn, delta) { var timeoutID = null; return function() { clearTimeout(timeoutID); var args = arguments; timeoutID = setTimeout(function() { fn.apply(null, args); }, delta); }; } var debouncedLog = debounce(log, delta); window.onkeydown = debouncedLog; 4. Prevent context loss

Good, good. Up until now, we can convert any number of functions into debounced versions of themselves, along with passing the arguments. But there’s another problem that arises - context loss.

Since we are using .apply method of a function, we are providing the context (a.k.a. the this variable) for function invocation. This is a problem for any function that depends on this internally.

To resolve this problem, all we need is the proper context of the function being debounced. But we can’t extract the context from the function itself. Therefore, it must be provided externally.

// ... function debounce(fn, delta, context) { // ... fn.apply(context, args); // ... } Conclusion

Finally, what arrive at the this code for Debounce. You can try it here and here .

Debounce function debounce(fn, delta, context) { var timeoutID = null; return function() { clearTimeout(timeoutID); var args = arguments; timeoutID = setTimeout(function() { fn.apply(context, args); }, delta); }; }

And here are the rest of the techniques:

Immediate function immediate(fn, delta, context) { var timeoutID = null; var safe = true; return function() { var args = arguments; if (safe) { fn.call(context, args); safe = false; } clearTimeout(timeoutID); timeoutID = setTimeout(function() { safe = true; }, delta); }; } Throttle function throttle(fn, delta, context) { var safe = true; return function() { var args = arguments; if (safe) { fn.call(context, args); safe = false; setTimeout(function() { safe = true; }, delta); } }; }

And here’s a tiny demo of all three techniques.

In the next, and final, post of the series, we will see a native implementation of Throttle that works well with browsers.

Stay tuned.

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

分页:12
转载请注明
本文标题:Timing Controls - Part 2
本站链接:http://www.codesec.net/view/481612.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(38)