Functions 2 Flashcards

1
Q

What does this code do?

function sayHi() {
  console.log('Hello');
}

setTimeout(sayHi(), 1000);

A

error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
What does this code do?
function sayHi() {
  console.log('Hello');
}

setTimeout(sayHi, 1000);

A

logs “hello” after 1 second

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. Do a function after a set amount of time

2. Keep doing a function on an interval

A
  1. let timerId = setTimeout(func|code, [delay], [funcArg1], [funcArg2], …)
  2. let timerId = setInterval(func|code, [delay], [funcArg1], [funcArg2], …)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does
setTimeout()
do or return?

A

Does a function after a set amount of time

Returns an ID unique to that instance of setTimeout that can be used to cancel the timer
let timerId = setTimeout(func|code, [delay], [funcArg1], [funcArg2], ...)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What can go into the setTimeout() function parameter?

A
  1. Callback function name
  2. Function expression
  3. A string with code that will be executed (like with new function. Don’t do it though)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
What does this code do?
let variable = setTimeout(...);
A

Returns a “timer identifier” timerId that we can use to cancel the execution.
let timerId = setTimeout(() => alert(“never happens”), 1000);
alert(timerId); // timer identifier

clearTimeout(timerId);
alert(timerId); // same identifier (doesn’t become null after canceling)

BUT

It can depend on the environment. Some environments return the setTimeout object with more methods on it
There is no universal specification for these methods, so that’s fine.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
What does this code do?
function sayHi() {
  console.log('Hello');
}

setInterval(sayHi, 1000);

A

logs “hello” every 1 second

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Create an repeating interval where a function will execute.

Cancel that interval after a set number of seconds

A
// repeat with the interval of 2 seconds
let timerId = setInterval(() => alert('tick'), 2000);
// after 5 seconds stop
setTimeout(() => { 
  clearInterval(timerId); 
  alert('stop'); 
}, 5000);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Create an interal using setTimeout instead of setInterval()

A
let timerId = setTimeout(function tick() {
  alert('tick');
  timerId = setTimeout(tick, 2000); // (*)
}, 2000);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is 1 benefit to using a setTimeout interval vs setInterval and 1 difference

A

Benefit:
with setTimeout we can effect the interval as it goes.

Difference:
setInterval executes the function every xms regardless of the time it takes to execute the function.
If it takes longer than xms it executes the next round right away,
setTimeout executes the next round as part of the code and can be made to wait till the code executes before performing the next round.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why must you always run clear interval on setInterval/setTimeout?

A

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.

For setInterval the function stays in memory until clearInterval is called.

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
What is returned from this code
setTimeout(() => console.log("Hello"));
console.log("World");
OR
setTimeout(() => console.log("Hello"), 0);
console.log("World");

How about:
setTimeout(() => console.log(“Hello”), 100);
console.log(“World”);

A

World Hello all 3 cases

This schedules the execution of the function as soon as possible. But the scheduler will invoke it only after the CURRENTLY EXECUTING script is complete.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does this code:

function printNumbersB(from, to) {
  function logger(x) {
    console.log(x);
    if (x > to) {
      return;
    }
    setTimeout(logger, 10, ++from);
  }
  logger(from, to); 
 }

Differ from this code:

function printNumbersB(from, to) {
  function logger(x) {
    console.log(x);
  }
  if (from > to) {
    return;
  } else {
    setTimeout(logger, 10, from);
    printNumbersB(++from, to);  
  }
}
A

The top code logs numbers, sets a timer to log a number and set another timer.
A number is logged every 10 ms.

The bottom code sets a time to log a number and immediately goes to the next recursive step.
The numbers are spewed out (basically) all at once after 10 ms

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When will the scheduled function run?

  • After the loop.
  • Before the loop.
  • In the beginning of the loop.

What is alert going to show?

let i = 0;
setTimeout(() => alert(i), 100); 
for(let j = 0; j < 100000000; j++) {
  i++;
}

how about with

let i = 0;
setInterval(() => alert(i), 100); 
for(let j = 0; j < 100000000; j++) {
  i++;
}
A

After the loop

It will alert 100000000 100 ms after the loop

Same thing with setInterval. It starts logging 100000000 every 100ms after the loop has executed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does this return?
let initGame = () => ({
level: 1,
score: 0
});

A

Returns an object: {
level: 1,
score: 0
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does this return?
let initGame = () => {
level: 1,
score: 0
};

A

An error.
It doesn’t read it as an object because it’s on multiple lines.

17
Q

What does this return?
let initGame = () => { level: 1, score: 0 };

A

Returns an object: {
level: 1,
score: 0
}