From 225b131fcffc8475a0fb35c7534a935ad93ec361 Mon Sep 17 00:00:00 2001 From: MaHdi Date: Thu, 23 Sep 2021 12:57:49 +0330 Subject: [PATCH 01/12] Translate a part of article --- .../08-settimeout-setinterval/article.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/1-js/06-advanced-functions/08-settimeout-setinterval/article.md b/1-js/06-advanced-functions/08-settimeout-setinterval/article.md index 984102687..cf0572ae9 100644 --- a/1-js/06-advanced-functions/08-settimeout-setinterval/article.md +++ b/1-js/06-advanced-functions/08-settimeout-setinterval/article.md @@ -1,13 +1,13 @@ -# Scheduling: setTimeout and setInterval +# زمان‌بندی: setTimeout و setInterval -We may decide to execute a function not right now, but at a certain time later. That's called "scheduling a call". +شاید ما تصمیم بگیریم که یک تابع را همین الان اجرا نکنیم اما در زمان مشخصی در آینده اجرا کنیم. به این کار «زمان‌بندی فراخوانی» می‌گویند. -There are two methods for it: +دو متد برای آن وجود دارد: -- `setTimeout` allows us to run a function once after the interval of time. -- `setInterval` allows us to run a function repeatedly, starting after the interval of time, then repeating continuously at that interval. +- `setTimeout` به ما اجازه می‌دهد تا یک تابع را بعد از مدتی یک بار اجرا کنیم. +- `setInterval` به ما اجازه می‌دهد که یک تابع را به صورت تکرار شونده اجرا کنیم که بعد از آن مدت زمان فراخوانی شروع می‌شود و سپس به طور پیوسته با همان فاصله زمانی تکرار می‌شود. -These methods are not a part of JavaScript specification. But most environments have the internal scheduler and provide these methods. In particular, they are supported in all browsers and Node.js. +این متدها جزء مشخصات جاوااسکریپت نیستند. اما اکثر محیط‌ها زمان‌بند درونی دارند و این متدها را فراهم می‌کنند. خصوصا، این متدها در تمام مرورگرها و Node.js پشتیبانی می‌شوند. ## setTimeout From 86e12d23c9d448d1512a0ce5b2f5e8b59637f93b Mon Sep 17 00:00:00 2001 From: MaHdi Date: Fri, 24 Sep 2021 17:51:45 +0330 Subject: [PATCH 02/12] Translate a part of article --- .../08-settimeout-setinterval/article.md | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/1-js/06-advanced-functions/08-settimeout-setinterval/article.md b/1-js/06-advanced-functions/08-settimeout-setinterval/article.md index cf0572ae9..b1c21728b 100644 --- a/1-js/06-advanced-functions/08-settimeout-setinterval/article.md +++ b/1-js/06-advanced-functions/08-settimeout-setinterval/article.md @@ -9,31 +9,31 @@ این متدها جزء مشخصات جاوااسکریپت نیستند. اما اکثر محیط‌ها زمان‌بند درونی دارند و این متدها را فراهم می‌کنند. خصوصا، این متدها در تمام مرورگرها و Node.js پشتیبانی می‌شوند. -## setTimeout +## تابع setTimeout -The syntax: +سینتکس: ```js let timerId = setTimeout(func|code, [delay], [arg1], [arg2], ...) ``` -Parameters: +پارامترها: `func|code` -: Function or a string of code to execute. -Usually, that's a function. For historical reasons, a string of code can be passed, but that's not recommended. +: تابع یا رشته‌ای از کد برای اجرا. +معمولا یک تابع است. بنا به دلایلی مربوط به گذشته، یک رشته از کد را هم می‌توان قرار داد اما پیشنهاد نمی‌شود. `delay` -: The delay before run, in milliseconds (1000 ms = 1 second), by default 0. +: میزان تاخیر قبل از اجرا، به میلی‌ثانیه (1000 میلی‌ثانیه = 1 ثانیه)، به طور پیش‌فرض 0 است. `arg1`, `arg2`... -: Arguments for the function (not supported in IE9-) +: آرگومان‌های تابع (در IE9- پشتیبانی نمی‌شود) -For instance, this code calls `sayHi()` after one second: +برای مثال، این کد `sayHi()` را بعد از یک ثانیه فرا می‌خواند: ```js run function sayHi() { - alert('Hello'); + alert('سلام'); } *!* @@ -41,40 +41,40 @@ setTimeout(sayHi, 1000); */!* ``` -With arguments: +با آرگومان‌ها: ```js run function sayHi(phrase, who) { - alert( phrase + ', ' + who ); + alert( phrase + '، ' + who ); } *!* -setTimeout(sayHi, 1000, "Hello", "John"); // Hello, John +setTimeout(sayHi, 1000, "سلام", "John"); // John ،سلام */!* ``` -If the first argument is a string, then JavaScript creates a function from it. +اگر اولین آرگومان رشته باشد، سپس جاوااسکریپت یک تابع از آن می‌سازد. -So, this will also work: +پس این کار می‌کند: ```js run no-beautify -setTimeout("alert('Hello')", 1000); +setTimeout("alert('سلام')", 1000); ``` -But using strings is not recommended, use arrow functions instead of them, like this: +اما استفاده از رشته‌ها پیشنهاد نمی‌شود، به جای آنها از تابع‌های کمانی استفاده کنید، مانند اینجا: ```js run no-beautify -setTimeout(() => alert('Hello'), 1000); +setTimeout(() => alert('سلام'), 1000); ``` -````smart header="Pass a function, but don't run it" -Novice developers sometimes make a mistake by adding brackets `()` after the function: +````smart header="تابع را رد کنید، اما آن را فراخوانی نکنید" +توسعه‌دهندگان بی‌تجربه گاهی اوقات با اضافه کردن پرانتز `()` بعد از تابع دچار اشتباه می‌شوند: ```js -// wrong! +// !اشتباه است setTimeout(sayHi(), 1000); ``` -That doesn't work, because `setTimeout` expects a reference to a function. And here `sayHi()` runs the function, and the *result of its execution* is passed to `setTimeout`. In our case the result of `sayHi()` is `undefined` (the function returns nothing), so nothing is scheduled. +این کار نمی‌کند چون `setTimeout` توقع رجوع به تابع را دارد. و اینجا `sayHi()` تابع را اجرا می‌کد و *نتیجه اجرا شدن آن* به `setTimeout` فرستاده می‌شود. در این مورد ما، نتیجه `sayHi()` برابر با `undefined` است (تابع چیزی را برنمی‌گرداند) پس چیزی زمان‌بندی نمی‌شود. ```` ### Canceling with clearTimeout From f2864ae5e70d0c4b85042af955715835630443e3 Mon Sep 17 00:00:00 2001 From: MaHdi Date: Fri, 24 Sep 2021 22:33:04 +0330 Subject: [PATCH 03/12] Translate a part of article --- .../08-settimeout-setinterval/article.md | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/1-js/06-advanced-functions/08-settimeout-setinterval/article.md b/1-js/06-advanced-functions/08-settimeout-setinterval/article.md index b1c21728b..b91777fac 100644 --- a/1-js/06-advanced-functions/08-settimeout-setinterval/article.md +++ b/1-js/06-advanced-functions/08-settimeout-setinterval/article.md @@ -77,32 +77,32 @@ setTimeout(sayHi(), 1000); این کار نمی‌کند چون `setTimeout` توقع رجوع به تابع را دارد. و اینجا `sayHi()` تابع را اجرا می‌کد و *نتیجه اجرا شدن آن* به `setTimeout` فرستاده می‌شود. در این مورد ما، نتیجه `sayHi()` برابر با `undefined` است (تابع چیزی را برنمی‌گرداند) پس چیزی زمان‌بندی نمی‌شود. ```` -### Canceling with clearTimeout +### لغو کردن با clearTimeout -A call to `setTimeout` returns a "timer identifier" `timerId` that we can use to cancel the execution. +فراخوانی `setTimeout` یک «شناسه‌ی تایمر» `timerId` را برمی‌گرداند که ما می‌توانیم برای لغو کردن اجرا شدن از آن استفاده کنیم. -The syntax to cancel: +سینتکس برای لغو کردن: ```js let timerId = setTimeout(...); clearTimeout(timerId); ``` -In the code below, we schedule the function and then cancel it (changed our mind). As a result, nothing happens: +در کد پایین، ما اجرای تابع را زمان‌بندی می‌کنیم و سپس آن را لغو می‌کنیم (تصمیم دیگری گرفتیم). در نتیجه، چیزی اتفاق نمی‌افتد: ```js run no-beautify -let timerId = setTimeout(() => alert("never happens"), 1000); -alert(timerId); // timer identifier +let timerId = setTimeout(() => alert("هیچوقت رخ نمی‌دهد"), 1000); +alert(timerId); // شناسه‌ی تایمر clearTimeout(timerId); -alert(timerId); // same identifier (doesn't become null after canceling) +alert(timerId); // (نمی‌شود null بعد از لغو کردن) شناسه یکسان ``` -As we can see from `alert` output, in a browser the timer identifier is a number. In other environments, this can be something else. For instance, Node.js returns a timer object with additional methods. +همانطور که از خروجی `alert` می‌بینیم، در یک مرورگر، شناسه‌ی تایمر یک عدد است. در محیط‌های دیگر، این می‌تواند چیز دیگری باشد. برای مثال، Node.js یک شیء تایمر همراه با متدهای اضافی را برمی‌گرداند. -Again, there is no universal specification for these methods, so that's fine. +باز هم، مشخصات جامعی برای این متدها وجود ندارد پس مشکلی نیست. -For browsers, timers are described in the [timers section](https://www.w3.org/TR/html5/webappapis.html#timers) of HTML5 standard. +برای مرورگرها، تایمرها در [قسمت تایمرهای](https://www.w3.org/TR/html5/webappapis.html#timers) استاندارد HTML5 هستند. ## setInterval From 8e9a8c0de6875a30e48f9856c80fe822cd7907f8 Mon Sep 17 00:00:00 2001 From: MaHdi Date: Sat, 25 Sep 2021 12:58:37 +0330 Subject: [PATCH 04/12] Translate a part of article --- .../08-settimeout-setinterval/article.md | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/1-js/06-advanced-functions/08-settimeout-setinterval/article.md b/1-js/06-advanced-functions/08-settimeout-setinterval/article.md index b91777fac..ac6df2b9d 100644 --- a/1-js/06-advanced-functions/08-settimeout-setinterval/article.md +++ b/1-js/06-advanced-functions/08-settimeout-setinterval/article.md @@ -104,32 +104,32 @@ alert(timerId); // (نمی‌شود null بعد از لغو کردن) شناسه برای مرورگرها، تایمرها در [قسمت تایمرهای](https://www.w3.org/TR/html5/webappapis.html#timers) استاندارد HTML5 هستند. -## setInterval +## تابع setInterval -The `setInterval` method has the same syntax as `setTimeout`: +تابع `setInterval` سینتکس مشابهی با `setTimeout` دارد: ```js let timerId = setInterval(func|code, [delay], [arg1], [arg2], ...) ``` -All arguments have the same meaning. But unlike `setTimeout` it runs the function not only once, but regularly after the given interval of time. +تمام آرگومان‌ها معنی یکسانی دارند. اما برخلاف `setTimeout` تابع را نه تنها یک بار بلکه بعد از مدت زمان داده شده به طور منظم اجرا می‌کند. -To stop further calls, we should call `clearInterval(timerId)`. +برای متوقف کردن فراخوانی‌های بیشتر، ما باید `clearInterval(timerId)` را فراخوانی کنیم. -The following example will show the message every 2 seconds. After 5 seconds, the output is stopped: +مثال پایین پیام را هر 2 ثانیه نشان می‌دهد. بعد از 5 ثانیه، خروجی متوقف می‌شود: ```js run -// repeat with the interval of 2 seconds +// با فاصله 2 ثانیه تکرار می‌شود let timerId = setInterval(() => alert('tick'), 2000); -// after 5 seconds stop +// بعد از 5 ثانیه متوقف می‌شود setTimeout(() => { clearInterval(timerId); alert('stop'); }, 5000); ``` -```smart header="Time goes on while `alert` is shown" -In most browsers, including Chrome and Firefox the internal timer continues "ticking" while showing `alert/confirm/prompt`. +```smart header="هنگامی که `alert` نمایش داده می‌شود زمان می‌گذرد" +در اکثر مرورگرها که شامل Chrome و Firefox هم می‌شود، تایمر درونی در حین نمایش `alert/confirm/prompt` به «تیک خوردن» ادامه می‌دهد. -So if you run the code above and don't dismiss the `alert` window for some time, then the next `alert` will be shown immediately as you do it. The actual interval between alerts will be shorter than 2 seconds. +بنابراین اگر شما کد بالا را اجرا کنید و برای چند ثانیه پنجره `alert` را رد نکنید، سپس `alert` بعدی بلافاصله بعد از اینکه آن را رد کنید نمایش داده می‌شود. فاصله زمانی واقعی بین alertها کوتاه‌تر از 2 ثانیه خواهد بود. ``` ## Nested setTimeout From 2dedb0af2cf12f8c0c527752010afd28f8b6d391 Mon Sep 17 00:00:00 2001 From: MaHdi Date: Sun, 26 Sep 2021 11:11:58 +0330 Subject: [PATCH 05/12] Translate a part of article --- .../08-settimeout-setinterval/article.md | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/1-js/06-advanced-functions/08-settimeout-setinterval/article.md b/1-js/06-advanced-functions/08-settimeout-setinterval/article.md index ac6df2b9d..de5755f37 100644 --- a/1-js/06-advanced-functions/08-settimeout-setinterval/article.md +++ b/1-js/06-advanced-functions/08-settimeout-setinterval/article.md @@ -132,14 +132,14 @@ setTimeout(() => { clearInterval(timerId); alert('stop'); }, 5000); بنابراین اگر شما کد بالا را اجرا کنید و برای چند ثانیه پنجره `alert` را رد نکنید، سپس `alert` بعدی بلافاصله بعد از اینکه آن را رد کنید نمایش داده می‌شود. فاصله زمانی واقعی بین alertها کوتاه‌تر از 2 ثانیه خواهد بود. ``` -## Nested setTimeout +## تابع setTimeout تودرتو -There are two ways of running something regularly. +دو راه برای انجام چیزی به طور منظم و پی در پی وجود دارد. -One is `setInterval`. The other one is a nested `setTimeout`, like this: +یکی از آنها `setInterval` است. راه دیگر یک `setTimeout` تودرتو است، مانند این: ```js -/** instead of: +/** :به جای این let timerId = setInterval(() => alert('tick'), 2000); */ @@ -151,21 +151,21 @@ let timerId = setTimeout(function tick() { }, 2000); ``` -The `setTimeout` above schedules the next call right at the end of the current one `(*)`. +تابع `setTimeout` بالا فراخوانی بعدی را درست برای انتهای فراخوانی کنونی `(*)` زمان‌بندی می‌کند. -The nested `setTimeout` is a more flexible method than `setInterval`. This way the next call may be scheduled differently, depending on the results of the current one. +`setTimeout` تودرتو نسبت به `setInterval` انعطاف بیشتری دارد. در این روش بسته به نتایج فراخوانی کنونی، فراخوانی بعدی ممکن است زمان‌بندی متفاوتی داشته باشد. -For instance, we need to write a service that sends a request to the server every 5 seconds asking for data, but in case the server is overloaded, it should increase the interval to 10, 20, 40 seconds... +برای مثال، ما نیاز داریم که سرویسی بنویسیم تا هر 5 ثانیه یک درخواست به سرور بفرستد و برای داده درخواست کند اما در درصورتی که سرور شلوغ باشد، باید فاصله زمانی را به 10، 20، 40 ثانیه افزایش دهد... -Here's the pseudocode: +اینجا یک شبه کد داریم: ```js let delay = 5000; let timerId = setTimeout(function request() { - ...send request... + ...فرستادن درخواست... - if (request failed due to server overload) { - // increase the interval to the next run + if (درخواست به دلیل شلوغی سرور شکست خورد) { + // فاصله زمانی را در فراخوانی بعدی افزایش دهید delay *= 2; } @@ -175,11 +175,11 @@ let timerId = setTimeout(function request() { ``` -And if the functions that we're scheduling are CPU-hungry, then we can measure the time taken by the execution and plan the next call sooner or later. +و اگر تابع‌هایی که ما زمان‌بندی می‌کنیم از پردازنده زیاد استفاده می‌کنند، می‌توانیم زمانی که توسط یک بار اجرا شدن نیاز است را اندازه بگیریم و سپس فراخوانی بعدی را زودتر یا دیرتر زمان‌بندی کنیم. -**Nested `setTimeout` allows to set the delay between the executions more precisely than `setInterval`.** +**`setTimeout` تودرتو به ما اجازه می‌دهد که فاصله زمانی بین فراخوانی‌ها را نسبت به `setInterval` دقیق‌تر تنظیم کنیم.** -Let's compare two code fragments. The first one uses `setInterval`: +بیایید دو قطعه کد را مقایسه کنیم. اولی از `setInterval` استفاده می‌کند: ```js let i = 1; @@ -188,7 +188,7 @@ setInterval(function() { }, 100); ``` -The second one uses nested `setTimeout`: +دومی از `setTimeout` تودرتو استفاده می‌کند: ```js let i = 1; @@ -198,41 +198,41 @@ setTimeout(function run() { }, 100); ``` -For `setInterval` the internal scheduler will run `func(i++)` every 100ms: +در `setInterval` زمان‌بند داخلی `func(i++)` را هر 100 میلی‌ثانیه اجرا می‌کند: ![](setinterval-interval.svg) -Did you notice? +آیا متوجه شدید? -**The real delay between `func` calls for `setInterval` is less than in the code!** +**فاصله زمانی واقعی بین فراخوانی‌های `func` برای `setInterval` کمتر از زمان موجود در کد است!** -That's normal, because the time taken by `func`'s execution "consumes" a part of the interval. +این موضوع عادی است چون مدت زمانی که برای اجرای `func` صرف می‌شود بخشی از فاصله زمانی را «اشغال می‌کند». -It is possible that `func`'s execution turns out to be longer than we expected and takes more than 100ms. +ممکن است اجرای `func` از زمانی که ما توقع داشتیم بیشتر طول بکشد و بیشتر از 100 میلی‌ثانیه زمان ببرد. -In this case the engine waits for `func` to complete, then checks the scheduler and if the time is up, runs it again *immediately*. +در این صورت موتور صبر می‌کند تا اجرای `func` کامل شود سپس زمان‌بند را بررسی می‌کند و اگر زمان فراخوانی رسیده باشد، *بلافاصله* آن را دوباره اجرا می‌کند. -In the edge case, if the function always executes longer than `delay` ms, then the calls will happen without a pause at all. +در مورد حساس، اگر اجرای تابع همیشه بیشتر از `delay` میلی‌ثانیه طول بکشد، سپس فراخوانی‌ها بدون اندکی مکث رخ می‌دهند. -And here is the picture for the nested `setTimeout`: +و اینجا تصویری برای `setTimeout` تودوتو داریم: ![](settimeout-interval.svg) -**The nested `setTimeout` guarantees the fixed delay (here 100ms).** +**`setTimeout` تودرتو فاصله زمانی ثابت را تضمین می‌کند (اینجا 100 میلی‌ثانیه).** -That's because a new call is planned at the end of the previous one. +به این دلیل که فراخوانی جدید در انتهای فراخوانی قبلی زمان‌بندی می‌شود. -````smart header="Garbage collection and setInterval/setTimeout callback" -When a function is passed in `setInterval/setTimeout`, an internal reference is created to it and saved in the scheduler. It prevents the function from being garbage collected, even if there are no other references to it. +````smart header="زباله‌روبی و فراخوانی setInterval/setTimeout" +زمانی که یک تابع در `setInterval/setTimeout` قرار داده شد، یک رجوع درونی به آن ساخته می‌شود و در زمان‌بند ذخیره می‌شود. این رجوع تابع را از زباله‌روبی نجات می‌دهد حتی اگر هیچ رجوع دیگری به آن وجود نداشته باشد. ```js -// the function stays in memory until the scheduler calls it +// تابع تا زمانی که زمان‌بند آن را فراخوانی کند درون حافظه می‌ماند setTimeout(function() {...}, 100); ``` -For `setInterval` the function stays in memory until `clearInterval` is called. +برای `setInterval` تابع تا زمانی که `clearInterval` فراخوانی شود درون حافظه می‌ماند. -There's a side-effect. A function references the outer lexical environment, so, while it lives, outer variables live too. They may take much more memory than the function itself. So when we don't need the scheduled function anymore, it's better to cancel it, even if it's very small. +یک عارضه جانبی وجود دارد. یک تابع به محیط لغوی بیرونی رجوع می‌کند پس، تا زمانی که تابع وجود داشته باشد، متغیرهای بیرونی هم وجود خواهند داشت. آنها حافظه بسیار بیشتری را نسبت به خود تابع اشغال می‌کنند. پس زمانی که دیگر نیازی به تابع زمان‌بندی شده نداریم، بهتر است که آن را لغو کنیم حتی اگر خیلی کوچک باشد. ```` ## Zero delay setTimeout From 46b9979de8ec69c0abbc41140465410dbec8534a Mon Sep 17 00:00:00 2001 From: MaHdi Date: Sun, 26 Sep 2021 19:55:33 +0330 Subject: [PATCH 06/12] Translate a part of article --- .../08-settimeout-setinterval/article.md | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/1-js/06-advanced-functions/08-settimeout-setinterval/article.md b/1-js/06-advanced-functions/08-settimeout-setinterval/article.md index de5755f37..635503317 100644 --- a/1-js/06-advanced-functions/08-settimeout-setinterval/article.md +++ b/1-js/06-advanced-functions/08-settimeout-setinterval/article.md @@ -235,15 +235,15 @@ setTimeout(function() {...}, 100); یک عارضه جانبی وجود دارد. یک تابع به محیط لغوی بیرونی رجوع می‌کند پس، تا زمانی که تابع وجود داشته باشد، متغیرهای بیرونی هم وجود خواهند داشت. آنها حافظه بسیار بیشتری را نسبت به خود تابع اشغال می‌کنند. پس زمانی که دیگر نیازی به تابع زمان‌بندی شده نداریم، بهتر است که آن را لغو کنیم حتی اگر خیلی کوچک باشد. ```` -## Zero delay setTimeout +## تابع setTimeout بدون تاخیر -There's a special use case: `setTimeout(func, 0)`, or just `setTimeout(func)`. +یک مورد استفاده خاص وجود دارد: `setTimeout(func, 0)` یا فقط `setTimeout(func)`. -This schedules the execution of `func` as soon as possible. But the scheduler will invoke it only after the currently executing script is complete. +این مورد اجرای `func` را برای نزدیک‌ترین موقع زمان‌بندی می‌کند. اما زمان‌بند آنرا بعد از اینکه اجرای اسکریپت کنونی تمام شد فرا می‌خواند. -So the function is scheduled to run "right after" the current script. +پس تابع زمان‌بندی می‌شود تا «درست بعد از» اسکریپت کنونی اجرا شود. -For instance, this outputs "Hello", then immediately "World": +برای مثال، این کد "Hello" را نمایش می‌دهد، سپس بلافاصله "World" را: ```js run setTimeout(() => alert("World")); @@ -251,37 +251,37 @@ setTimeout(() => alert("World")); alert("Hello"); ``` -The first line "puts the call into calendar after 0ms". But the scheduler will only "check the calendar" after the current script is complete, so `"Hello"` is first, and `"World"` -- after it. +خط اول «فراخوانی را بعد از 0 میلی‌ثانیه در تقویم» می‌گذارد. اما زمان‌بند فقط بعد از اینکه اسکریپت کنونی کامل شد «تقویم را بررسی می‌کند» پس `"Hello"` اول می‌آید و `"World"` بعد از آن. -There are also advanced browser-related use cases of zero-delay timeout, that we'll discuss in the chapter . +همچنین موارد استفاده پیشرفته مربوط به مرورگر از زمان‌بندی با تاخیر 0 وجود دارند که ما در فصل به آنها می‌پردازیم. -````smart header="Zero delay is in fact not zero (in a browser)" -In the browser, there's a limitation of how often nested timers can run. The [HTML5 standard](https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timers) says: "after five nested timers, the interval is forced to be at least 4 milliseconds.". +````smart header="تاخیر صفر در واقع صفر نیست (در مرورگر)" +در مرورگر، یک محدودیت برای اینکه تایمرهای تودرتو هر چند وقت یک بار می‌توانند اجرا شوند وجود دارد. [استاندارد HTML5](https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timers) می‌گوید: «بعد از 5 تایمر تودرتو، فاصله زمانی ناچار می‌شود که حداقل 4 میلی‌ثانیه باشد.». -Let's demonstrate what it means with the example below. The `setTimeout` call in it re-schedules itself with zero delay. Each call remembers the real time from the previous one in the `times` array. What do the real delays look like? Let's see: +بیایید با مثال پایین نشان دهیم که این یعنی چه. فراخوانی `setTimeout` در مثال زیر خودش را با تاخیر صفر دوباره زمان‌بندی می‌کند. هر فراخوانی زمان واقعی گذشته از فراخوانی قبلی را در آرایه `times` ذخیره می‌کند. تاخیرهای واقعی چگونه بنظر می‌رسند؟ بیایید ببینیم: ```js run let start = Date.now(); let times = []; setTimeout(function run() { - times.push(Date.now() - start); // remember delay from the previous call + times.push(Date.now() - start); // فاصله زمانی از فراخوانی قبلی را به یاد می‌سپارد - if (start + 100 < Date.now()) alert(times); // show the delays after 100ms - else setTimeout(run); // else re-schedule + if (start + 100 < Date.now()) alert(times); // فاصله‌های زمانی را بعد از 100 میلی‌ثانیه نشان می‌دهد + else setTimeout(run); // در غیر این صورت دوباره زمان‌بندی می‌کند }); -// an example of the output: +// :مثالی از خروجی // 1,1,1,1,9,15,20,24,30,35,40,45,50,55,59,64,70,75,80,85,90,95,100 ``` -First timers run immediately (just as written in the spec), and then we see `9, 15, 20, 24...`. The 4+ ms obligatory delay between invocations comes into play. +ابتدا تایمرها بلافاصله اجرا می‌شوند (همانطور که در مشخصات نوشته شده) و سپس ما `...24 ,20 ,15 ,9` را می‌بینیم. فاصله زمانیِ اجباریِ بیشتر از 4 میلی‌ثانیه برای فراخوانی‌ها وارد بازی می‌شود. -The similar thing happens if we use `setInterval` instead of `setTimeout`: `setInterval(f)` runs `f` few times with zero-delay, and afterwards with 4+ ms delay. +همچین چیزی اگر ما از `setInterval` به جای `setTimeout` استفاده کنیم رخ می‌دهد: `setInterval(f0` تابع `f` را چند بار با تاخیر صفر اجرا می‌کند و بعد از آن با تاخیر بیشتر از 4 میلی‌ثانیه. -That limitation comes from ancient times and many scripts rely on it, so it exists for historical reasons. +این محدودیت از قدیم وجود داشته و اسکریپت‌های زیادی بر آن تکیه کرده اند پس بنا به دلایلی مربوط به گذشته هنوز هم وجود دارد. -For server-side JavaScript, that limitation does not exist, and there exist other ways to schedule an immediate asynchronous job, like [setImmediate](https://nodejs.org/api/timers.html#timers_setimmediate_callback_args) for Node.js. So this note is browser-specific. +برای جاوااسکریپت سمت سرور، این محدودیت وجود ندارد و راه‌های دیگری برای زمان‌بندی یک کار ناهمزمان بدون تاخیر وجود دارند مانند [setImmediate](https://nodejs.org/api/timers.html#timers_setimmediate_callback_args) برای Node.js. پس این نکته فقط برای مرورگر است. ```` ## Summary From 828af6c6442a3587831b7d2195672164d650da69 Mon Sep 17 00:00:00 2001 From: MaHdi Date: Mon, 27 Sep 2021 12:16:22 +0330 Subject: [PATCH 07/12] Translate article Also changed the translations of two words --- .../08-settimeout-setinterval/article.md | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/1-js/06-advanced-functions/08-settimeout-setinterval/article.md b/1-js/06-advanced-functions/08-settimeout-setinterval/article.md index 635503317..7af970584 100644 --- a/1-js/06-advanced-functions/08-settimeout-setinterval/article.md +++ b/1-js/06-advanced-functions/08-settimeout-setinterval/article.md @@ -106,7 +106,7 @@ alert(timerId); // (نمی‌شود null بعد از لغو کردن) شناسه ## تابع setInterval -تابع `setInterval` سینتکس مشابهی با `setTimeout` دارد: +روش `setInterval` سینتکس مشابهی با `setTimeout` دارد: ```js let timerId = setInterval(func|code, [delay], [arg1], [arg2], ...) @@ -284,19 +284,19 @@ setTimeout(function run() { برای جاوااسکریپت سمت سرور، این محدودیت وجود ندارد و راه‌های دیگری برای زمان‌بندی یک کار ناهمزمان بدون تاخیر وجود دارند مانند [setImmediate](https://nodejs.org/api/timers.html#timers_setimmediate_callback_args) برای Node.js. پس این نکته فقط برای مرورگر است. ```` -## Summary +## خلاصه -- Methods `setTimeout(func, delay, ...args)` and `setInterval(func, delay, ...args)` allow us to run the `func` once/regularly after `delay` milliseconds. -- To cancel the execution, we should call `clearTimeout/clearInterval` with the value returned by `setTimeout/setInterval`. -- Nested `setTimeout` calls are a more flexible alternative to `setInterval`, allowing us to set the time *between* executions more precisely. -- Zero delay scheduling with `setTimeout(func, 0)` (the same as `setTimeout(func)`) is used to schedule the call "as soon as possible, but after the current script is complete". -- The browser limits the minimal delay for five or more nested calls of `setTimeout` or for `setInterval` (after 5th call) to 4ms. That's for historical reasons. +- روش‌های `setTimeout(func, delay, ...args)` و `setInterval(func, delay, ...args)` به ما اجازه‌دهند تا `func` را یکبار/به طور منظم بعد از `delay` میلی‌ثانیه اجرا کنیم. +- برای لغو کردن اجرا، ما باید `clearTimeout/clearInterval` را همراه با مقدار برگردانده شده توسط `setTimeout/setInterval` فراخوانی کنیم. +- فراخوانی‌های تودرتوی `setTimeout` جایگزینی منعطف‌تر برای `setInterval` هستند که به ما اجازه می‌دند تا زمان *بین* اجرا شدن‌ها را دقیق‌تر تنظیم کنیم. +- زمان‌بندی بدون تاخیر با `setTimeout(func, 0)` (مشابه با `setTimeout(func)`) برای اینکه فراخوانی را «در اسرع وقت اما بعد از اینکه اسکریپت کنونی کامل شد» زمان‌بندی کنیم استفاده می‌شود. +- مرورگر برای پنج یا بیشتر از پنج فراخوانی تودرتوی `setTimeout` یا `setInterval` (بعد از فراخوانی پنجم) حداقل فاصله زمانی را به 4 میلی‌ثانیه محدود می‌کند. دلیل آن هم مربوط به گذشته است. -Please note that all scheduling methods do not *guarantee* the exact delay. +لطفا در نظر داشته باشید که روش‌های زمان‌بندی فاصله زمانی دقیق را *تضمین* نمی‌کنند. -For example, the in-browser timer may slow down for a lot of reasons: -- The CPU is overloaded. -- The browser tab is in the background mode. -- The laptop is on battery. +برای مثال، تایمر درون مرورگر ممکن است به دلایل زیادی کند شود: +- کارهای زیادی به پردازنده سپرده شده است. +- تب (tab) مرورگر در حالت پس‌زمینه است. +- لپ تاپ در حال استفاده از باتری است. All that may increase the minimal timer resolution (the minimal delay) to 300ms or even 1000ms depending on the browser and OS-level performance settings. From b773d33f8a0e8a02b8a9c858ab35c5f11a589ca4 Mon Sep 17 00:00:00 2001 From: MaHdi Date: Mon, 27 Sep 2021 12:19:29 +0330 Subject: [PATCH 08/12] Translate task of "output-numbers" --- .../1-output-numbers-100ms/task.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/1-js/06-advanced-functions/08-settimeout-setinterval/1-output-numbers-100ms/task.md b/1-js/06-advanced-functions/08-settimeout-setinterval/1-output-numbers-100ms/task.md index 84bb0c39c..b9c6659f5 100644 --- a/1-js/06-advanced-functions/08-settimeout-setinterval/1-output-numbers-100ms/task.md +++ b/1-js/06-advanced-functions/08-settimeout-setinterval/1-output-numbers-100ms/task.md @@ -2,11 +2,11 @@ importance: 5 --- -# Output every second +# هر ثانیه را خروجی بگیرید -Write a function `printNumbers(from, to)` that outputs a number every second, starting from `from` and ending with `to`. +یک تابع `printNumbers(from, to)` بنویسید که هر ثانیه یک عدد را نمایش می‌دهد که از `from` شروع می‌شود و با `to` پایان می‌یابد. -Make two variants of the solution. +دو نوع راه‌حل بسازید. -1. Using `setInterval`. -2. Using nested `setTimeout`. +1. با استفاده از `setInterval`. +2. با استفاده از `setTimeout` تودرتو. From 038d8909813711a244117acb66d922c8790ff045 Mon Sep 17 00:00:00 2001 From: MaHdi Date: Mon, 27 Sep 2021 12:22:37 +0330 Subject: [PATCH 09/12] Translate solution of "output-numbers" --- .../1-output-numbers-100ms/solution.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/1-js/06-advanced-functions/08-settimeout-setinterval/1-output-numbers-100ms/solution.md b/1-js/06-advanced-functions/08-settimeout-setinterval/1-output-numbers-100ms/solution.md index b5b1da7a6..be39d2dfa 100644 --- a/1-js/06-advanced-functions/08-settimeout-setinterval/1-output-numbers-100ms/solution.md +++ b/1-js/06-advanced-functions/08-settimeout-setinterval/1-output-numbers-100ms/solution.md @@ -1,5 +1,5 @@ -Using `setInterval`: +با استفاده از `setInterval`: ```js run function printNumbers(from, to) { @@ -14,11 +14,11 @@ function printNumbers(from, to) { }, 1000); } -// usage: +// :کاربرد printNumbers(5, 10); ``` -Using nested `setTimeout`: +با استفاده از `setTimeout` تودرتو: ```js run @@ -34,13 +34,13 @@ function printNumbers(from, to) { }, 1000); } -// usage: +// :کاربرد printNumbers(5, 10); ``` -Note that in both solutions, there is an initial delay before the first output. The function is called after `1000ms` the first time. +در نظر داشته باشید که در هر دو راه‌حل، یک تاخیر اولیه قبل از اولین خروجی وجود دارد. تابع بعد `1000 میلی‌ثانیه` از اولین بار فراخوانی می‌شود. -If we also want the function to run immediately, then we can add an additional call on a separate line, like this: +اگر ما بخواهیم که تابع بلافاصله اجرا شود، سپس می‌توانیم یک فراخوانی اضافی در خطی جداگانه اضافه کنیم، مثل اینجا: ```js run function printNumbers(from, to) { From ac22e8cc985fae6c5d441cf70e648de9db643bc0 Mon Sep 17 00:00:00 2001 From: MaHdi Date: Mon, 27 Sep 2021 12:26:38 +0330 Subject: [PATCH 10/12] Translate task of "settimeout-result" --- .../4-settimeout-result/task.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/1-js/06-advanced-functions/08-settimeout-setinterval/4-settimeout-result/task.md b/1-js/06-advanced-functions/08-settimeout-setinterval/4-settimeout-result/task.md index 667c8ffa6..247b97bb8 100644 --- a/1-js/06-advanced-functions/08-settimeout-setinterval/4-settimeout-result/task.md +++ b/1-js/06-advanced-functions/08-settimeout-setinterval/4-settimeout-result/task.md @@ -2,25 +2,25 @@ importance: 5 --- -# What will setTimeout show? +# تابع setTimeout چه چیزی را نمایش خواهد داد? -In the code below there's a `setTimeout` call scheduled, then a heavy calculation is run, that takes more than 100ms to finish. +در کد پایین یک فراخوانی `setTimeout` زمان‌بندی شده وجود دارد سپس یک محاسبات سنگین اجرا می‌شود که بیشتر از 100 میلی‌ثانیه طول می‌کشد تا تمام شود. -When will the scheduled function run? +تابع زمان‌بندی شده چه زمانی اجرا می‌شود؟ -1. After the loop. -2. Before the loop. -3. In the beginning of the loop. +1. بعد از حلقه. +2. قبل از حلقه. +3. در آغاز حلقه. -What is `alert` going to show? +`alert` قرار است چه چیزی را نمایش دهد؟ ```js let i = 0; setTimeout(() => alert(i), 100); // ? -// assume that the time to execute this function is >100ms +// فرض کنیم که زمان اجرای این تابع بیشتر از 100 میلی‌ثانیه است for(let j = 0; j < 100000000; j++) { i++; } From 0378f89e0961fb18082291489219b6b0c05bcc73 Mon Sep 17 00:00:00 2001 From: MaHdi Date: Mon, 27 Sep 2021 12:28:48 +0330 Subject: [PATCH 11/12] Translate solution of "settimeout-result" --- .../4-settimeout-result/solution.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/1-js/06-advanced-functions/08-settimeout-setinterval/4-settimeout-result/solution.md b/1-js/06-advanced-functions/08-settimeout-setinterval/4-settimeout-result/solution.md index e652a3b36..8491a3c1f 100644 --- a/1-js/06-advanced-functions/08-settimeout-setinterval/4-settimeout-result/solution.md +++ b/1-js/06-advanced-functions/08-settimeout-setinterval/4-settimeout-result/solution.md @@ -1,14 +1,14 @@ -Any `setTimeout` will run only after the current code has finished. +هر تابع `setTimeout` فقط بعد از اینکه کد کنونی تمام شود اجرا می‌شود. -The `i` will be the last one: `100000000`. +متغیر `i` آخرین خواهد بود: `100000000`. ```js run let i = 0; setTimeout(() => alert(i), 100); // 100000000 -// assume that the time to execute this function is >100ms +// فرض کنیم که زمان اجرای این تابع بیشتر از 100 میلی‌ثانیه است for(let j = 0; j < 100000000; j++) { i++; } From fdba408c4ad258b17bafef1d754d50497814770a Mon Sep 17 00:00:00 2001 From: MaHdi Date: Mon, 27 Sep 2021 14:35:03 +0330 Subject: [PATCH 12/12] Apply suggestions from code review Co-authored-by: Mahdyar Hasanpour --- .../08-settimeout-setinterval/1-output-numbers-100ms/task.md | 2 +- .../08-settimeout-setinterval/4-settimeout-result/task.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/1-js/06-advanced-functions/08-settimeout-setinterval/1-output-numbers-100ms/task.md b/1-js/06-advanced-functions/08-settimeout-setinterval/1-output-numbers-100ms/task.md index b9c6659f5..769319be7 100644 --- a/1-js/06-advanced-functions/08-settimeout-setinterval/1-output-numbers-100ms/task.md +++ b/1-js/06-advanced-functions/08-settimeout-setinterval/1-output-numbers-100ms/task.md @@ -2,7 +2,7 @@ importance: 5 --- -# هر ثانیه را خروجی بگیرید +# در هر ثانیه خروجی بگیرید یک تابع `printNumbers(from, to)` بنویسید که هر ثانیه یک عدد را نمایش می‌دهد که از `from` شروع می‌شود و با `to` پایان می‌یابد. diff --git a/1-js/06-advanced-functions/08-settimeout-setinterval/4-settimeout-result/task.md b/1-js/06-advanced-functions/08-settimeout-setinterval/4-settimeout-result/task.md index 247b97bb8..0472958b2 100644 --- a/1-js/06-advanced-functions/08-settimeout-setinterval/4-settimeout-result/task.md +++ b/1-js/06-advanced-functions/08-settimeout-setinterval/4-settimeout-result/task.md @@ -13,7 +13,7 @@ importance: 5 3. در آغاز حلقه. -`alert` قرار است چه چیزی را نمایش دهد؟ +قرار است `alert` چه چیزی را نمایش دهد؟ ```js let i = 0;