|
1 | | -importance: 5 |
| 1 | +nilai penting: 5 |
2 | 2 |
|
3 | 3 | --- |
4 | 4 |
|
5 | 5 | # Debounce decorator |
6 | 6 |
|
7 | | -The result of `debounce(f, ms)` decorator is a wrapper that suspends calls to `f` until there's `ms` milliseconds of inactivity (no calls, "cooldown period"), then invokes `f` once with the latest arguments. |
| 7 | +Hasil dari dekorator `debounce(f, ms)` adalah sebuah pembungkus yang menghentikan pemanggilan `f` selama `ms` milidetik dari ketidakaktifan (tidak ada pemanggilan, "masa menunggu"), lalu memanggil `f` sekali dengan argumen terakhir. |
8 | 8 |
|
9 | | -In other words, `debounce` is like a secretary that accepts "phone calls", and waits until there's `ms` milliseconds of being quiet. And only then it transfers the latest call information to "the boss" (calls the actual `f`). |
| 9 | +Dengan kata lain, `debounce` seperti seorang sekertaris yang menerima "telefon", dan menunggu selama `ms` milidetik dari ketidakaktifan. Dan lalu menyampaikan pemanggilan terakhir kepada "boss" (melakukan pemanggilan `f`). |
10 | 10 |
|
11 | | -For instance, we had a function `f` and replaced it with `f = debounce(f, 1000)`. |
| 11 | +Contoh, jika kita mempunyai sebuah fungsi `f` dan lalu memasukan `f = debounce(f, 1000)`. |
12 | 12 |
|
13 | | -Then if the wrapped function is called at 0ms, 200ms and 500ms, and then there are no calls, then the actual `f` will be only called once, at 1500ms. That is: after the cooldown period of 1000ms from the last call. |
| 13 | +Maka jika fungsi pembungkus dipanggil pada 0ms, 200ms, dan 500ms, dan lalu tidak ada pemanggilan lainnya, maka fungsi `f` akan dipanggil sekali, pada 1500ms. Itulah: setelah beberapa saat fungsi tidak dipanggil maka fungsinya akan benar-benar dipanggil dengan rentang waktu 1000ms setelah pemanggilan terakhir. |
14 | 14 |
|
15 | 15 |  |
16 | 16 |
|
17 | | -...And it will get the arguments of the very last call, other calls are ignored. |
| 17 | +...Dan itu akan mendapatkan argumen dari pemanggilan yang paling terakhir, pemanggilan lainnya akan diabaikan. |
18 | 18 |
|
19 | | -Here's the code for it (uses the debounce decorator from the [Lodash library](https://lodash.com/docs/4.17.15#debounce): |
| 19 | +Ini adalah kodenya (digunakan untuk dekorator debounce dari [Lodash library](https://lodash.com/docs/4.17.15#debounce)): |
20 | 20 |
|
21 | 21 | ```js |
22 | 22 | let f = _.debounce(alert, 1000); |
23 | 23 |
|
24 | 24 | f("a"); |
25 | 25 | setTimeout( () => f("b"), 200); |
26 | 26 | setTimeout( () => f("c"), 500); |
27 | | -// debounced function waits 1000ms after the last call and then runs: alert("c") |
| 27 | +// fungsi debounce menunggu 1000ms setelah pemanggilan terakhir dan lalu menjalankan: alert("c") |
28 | 28 | ``` |
29 | 29 |
|
30 | | -Now a practical example. Let's say, the user types something, and we'd like to send a request to the server when the input is finished. |
| 30 | +Sekarang contoh yang lebih praktikal. Katakan, penggunakan mengetik sesuatu, dan kita ingin mengirim request kepada server ketika pengguna telah selesai mengetik. |
31 | 31 |
|
32 | | -There's no point in sending the request for every character typed. Instead we'd like to wait, and then process the whole result. |
| 32 | +Pada hal ini, sangat tidak berguna untuk mengirim request kepada server untuk setiap huruf yang diketik. Lagipula kita ingin menunggu, dan lalu memproses hasil ketikan pengguna. |
33 | 33 |
|
34 | | -In a web-browser, we can setup an event handler -- a function that's called on every change of an input field. Normally, an event handler is called very often, for every typed key. But if we `debounce` it by 1000ms, then it will be only called once, after 1000ms after the last input. |
| 34 | +Didalam peramban, kita bisa menyetel sebuah event handler(penangan event) -- sebuah fungsi yang dipanggi untuk setiap perubahan pada kotak inputan, sebuah penangan event dipanggil sangat sering untuk setiap huruf yang diketik. Tapi jika kita ingin men`debounce`nya selama 1000ms, maka fungsinya akan dipanggil sekali, 1000ms setelah penginputan huruf terakhir. |
35 | 35 |
|
36 | 36 | ```online |
37 | 37 |
|
38 | | -In this live example, the handler puts the result into a box below, try it: |
| 38 | +Didalam contoh ini, handlernya memasukan hasilnya kedalam kotak dibawah, cobalah: |
39 | 39 |
|
40 | 40 | [iframe border=1 src="debounce" height=200] |
41 | 41 |
|
42 | | -See? The second input calls the debounced function, so its content is processed after 1000ms from the last input. |
| 42 | +Lihat? inputan kedua memanggil fungsi debounce, jadi kontennya diproses setelah 1000ms dari inputan terakhir. |
43 | 43 | ``` |
44 | 44 |
|
45 | | -So, `debounce` is a great way to process a sequence of events: be it a sequence of key presses, mouse movements or something else. |
| 45 | +Jadi, `debounce` adalah cara terbaik untuk memproses event yang terjadi berurutan: bisa tombol yang dipencet berulang-ulang, pergerakan mouse atau lainnya. |
46 | 46 |
|
47 | | -It waits the given time after the last call, and then runs its function, that can process the result. |
| 47 | +Fungsinya akan menunggu hingga pemanggilan terakhir, dan lalu menjalankan fungsi aslinya, lalu hasilnya akan diolah. |
48 | 48 |
|
49 | | -The task is to implement `debounce` decorator. |
| 49 | +Tugasnya adalah untuk mengimplementasikan dekorator `debounce`. |
50 | 50 |
|
51 | | -Hint: that's just a few lines if you think about it :) |
| 51 | +Petunjuk: jika kamu perhatikan, perubahan fungsinya hanya dengan menambahkan beberapa baris :) |
0 commit comments