You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As we can tell from the above code, a function with a `*` would have the `next` function execution. In other words, the execution of the function returns an object. Every call to the `next` function can resume executing the paused code. A simple implementation of the Generator function is shown below:
1019
-
1018
+
Como podemos dizer no código acima, a função com um `*` teria a execução da função `next`. Em outras palavras, a execução de função retorna um objeto. Toda chamada a função `next` pode continuar a execução do código pausado. Um simples implementação da função Generator é mostrada abaixo:
1020
1019
```js
1021
1020
// cb is the compiled 'test' function
1021
+
// cb é a função 'test' compilada
1022
1022
functiongenerator(cb) {
1023
1023
return (function() {
1024
1024
var object = {
@@ -1038,23 +1038,23 @@ function generator(cb) {
1038
1038
};
1039
1039
})();
1040
1040
}
1041
-
//After babel's compilation, 'test' function turns into this:
1041
+
//Depois da compilação do babel's, a função 'test' retorna dentro dessa:
1042
1042
functiontest() {
1043
1043
var a;
1044
1044
returngenerator(function(_context) {
1045
1045
while (1) {
1046
1046
switch ((_context.prev=_context.next)) {
1047
-
// yield splits the code into several blocks
1048
-
//every 'next' call executes one block of clode
1049
-
//and indicates the next block to execute
1047
+
// yield separa o código em diversos blocos
1048
+
//cada chamada 'next' executa um bloco de código
1049
+
//e indica o próximo bloco a ser executado
1050
1050
case0:
1051
1051
a =1+2;
1052
1052
_context.next=4;
1053
1053
return2;
1054
1054
case4:
1055
1055
_context.next=6;
1056
1056
return3;
1057
-
//execution complete
1057
+
//execução completa
1058
1058
case6:
1059
1059
case"end":
1060
1060
return_context.stop();
@@ -1066,30 +1066,30 @@ function test() {
1066
1066
1067
1067
# Debouncing
1068
1068
1069
-
Have you ever encountered this problem in your development: how to do a complex computation in a scrolling event or to prevent the "second accidental click" on a button?
1069
+
Tendo você encontrado esse problema e seu dia-a-dia no desenvolvimento: como fazer uma computação complexa em um evento de scroll ou prevenir o "segundo clique acidental" no butão?
1070
1070
1071
-
These requirements can be achieved with function debouncing. Especially for the first one, if complex computations are carried out in frequent event callbacks, there's a large chance that the page becomes laggy. It's better to combine multiple computations into a single one, and only operate at particular time. Since there are many libraries that implement debouncing, we won't build our own here and will just take underscore's source code to explain debouncing:
1071
+
Esses requisitos podem ser alcançados com funcões debouncing. Especialmente para o primeiro, se uma computação complexa estiver sendo chamado em frequentes eventos de callbacks, existe uma grande chance que a página se torne lenta. É melhor combinar essas multiplas computações e uma, e apenas operar em determinado periodo de tempo. Desde que existe muitas bibliotecas que implementam debouncing, nós não construimos nosso próprio aqui e vamos pegar o código do underscore para explicar o debouncing:
1072
1072
1073
1073
```js
1074
1074
/**
1075
-
* underscore's debouncing function. When the callback function is called in series, func will only execute when the idel time is larger or equal to `wait`.
1075
+
* função underscore debouncing. Quando a função callback é chamada em série, a funcão vai executar apenas quando o tempo ideal é maior ou igual ao `wait`.
1076
1076
*
1077
-
* @param{function}funccallback function
1078
-
* @param{number}waitlength of waiting intervals
1079
-
* @param{boolean}immediatewhen set to true, func is executed immediately
1080
-
* @return{function}returns the function to be called by the client
1077
+
* @param{function}funcfunção callback
1078
+
* @param{number}waittamanho do intervalo de espera
1079
+
* @param{boolean}immediatequando definido para true, func é executada imadiatamente
1080
+
* @return{function}retorna a função a ser chamada pelo cliente
1081
1081
*/
1082
1082
_.debounce=function(func, wait, immediate) {
1083
1083
var timeout, args, context, timestamp, result;
1084
1084
1085
1085
varlater=function() {
1086
-
//compare now to the last timestamp
1086
+
//compara now para o último timestamp
1087
1087
var last =_.now() - timestamp;
1088
-
//if the current time interval is smaller than the set interval and larger than 0, then reset the timer.
1088
+
//se o tempo de intervalo atual é menor então o set interval é maior que 0, então reinicie o timer.
1089
1089
if (last < wait && last >=0) {
1090
1090
timeout =setTimeout(later, wait - last);
1091
1091
} else {
1092
-
//otherwise it's time to execute the callback function
A implementação completa da ƒunção não é tão difícil.
1122
+
1123
+
- Para a implementação de proteger contra clicks acidentais: enquanto eu começar o time e o time existir, não importa quantas vezes eu clicar o butão, a função de callback não será executada. Contudo quando o time termina, é setado para `null`, outro click é permitido.
1124
+
-
1125
+
1121
1126
The complete function implementation is not too difficult. Let's summarize here.
1122
1127
1123
1128
- For the implementation of protecting against accidental clicks: as long as I start a timer and the timer is there, no matter how you click the button, the callback function won't be executed. Whenever the timer ends and is set to `null`, another click is allowed.
0 commit comments