Skip to content

Commit 83e1d30

Browse files
authored
Set timeout and set interval documents (#182)
* Began writing set-interval document
1 parent 092b3ae commit 83e1d30

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

en/functions/set-interval.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Set Interval
2+
The set Interval method is a method used to call a function and add a delay time to it, in milliseconds, before the function will run again. For example, if you're making a function that generates a random color, you can use `setInterval()` to say how long the computer has to wait before the function runs again and generates another color. This is useful in making functions repeat.
3+
4+
The first parameter in the method is the name of the function you're setting an interval to, the second parameter is for how long the interval will be. You can add extra parameters in case you want to add the parameters of a function.
5+
6+
As another simple example, let's create a function called `repeatSaying` where it says "And again!" every 2 seconds in the [console](https://javascript.sumankunwar.com.np/en/exercises/console.html).
7+
8+
```js
9+
function repeatSaying() {
10+
console.log("And again");
11+
}
12+
//when called, it generates in the console: "And again!"
13+
14+
setInterval(repeatSaying, 2000);
15+
//calls the function every 2 seconds
16+
17+
18+
```
19+
You can also add parameters of a function when you use set interval. Continuing on with the previous example let's add an ellipsis to the console statement, to show that it repeats. First we'll add a parameter called `el` which is short for ellipse. Next we'll add a `+` followed by calling are parameter `el` to show that the value of the parameter comes after. Finally in set interval let's add a comma `,` followed by a string for the value of the ellipse parameter, we'll put `"..."`.
20+
21+
```js
22+
function repeatSaying(el) {
23+
console.log("And again!" + el);
24+
}
25+
26+
setInterval(repeatSaying, 2000, "...");
27+
//When it runs, it'll repeat the saying "And again!..."
28+
```
29+
30+
As you can see from this example, after you put the function and interval for the function, you can set the values of the function parameters inside set interval.
31+
32+
33+
34+
35+
## Clear Interval
36+
You can use the `clearInterval()` method to remove a set interval with a specefic variable name. As an example based on the previous one let's store set interval into a variable named `intervalTime`, however, right after our variable we'll call it inside clear interval by writing `clearInterval(intervalTime).`
37+
38+
```js
39+
function repeatSaying(el) {
40+
console.log("And again!" + el);
41+
}
42+
43+
var interval = setInterval(repeatSaying, 2000, "...");
44+
45+
clearInterval(interval);
46+
//The clear Interval method stops setInterval
47+
```
48+
49+
When this code is run, you'll see that there is no output. This is because `setInterval` was the only thing calling the `repeatSaying` function, but since it was removed by `clearInterval` it's no longer is called. Even if it was called seperately using `repeatSaying()` it would only run once because clear Interval stops it from repeating.
50+

en/functions/set-timeout.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Set Timeout
2+
The set timeout global method is used to add a timer (in milliseconds) before a function is ran.
3+
4+
For instance, in this example after "Ready..." is written in the console, the function `start()` has to wait 3 seconds before running.
5+
6+
```js
7+
console.log("Ready...");
8+
9+
function start() {
10+
console.log("go!!");
11+
}
12+
13+
setTimeout(start, 3000);
14+
15+
//Output: "Ready..." then after 3 seconds, "go!!"
16+
```
17+
18+
# Clear Timeout
19+
The clear timeout global method is used to remove any `setTimeout()` methods that are stores in variables. For instance let's change our last example by storing `setTimeout()` in a variable
20+
```js
21+
console.log("Ready...");
22+
23+
function start() {
24+
console.log("go!!");
25+
}
26+
27+
let timeBeforeStart = setTimeout(start, 3000);
28+
29+
clearTimeout(timeBeforeStart);
30+
// Stops the function as a whole from running
31+
```

0 commit comments

Comments
 (0)