CP2 - Lock date tomorrow at 11PM - great work so far!
HW2
Callbacks: What are JS functions really?
Timers review
Think back to your first programming language (for most of you, Java)
Unlike an object-oriented language like Java, with JS we:
let
vs. int
)if (id("my-box")) { ... // true if exists on the page }
Java is often used to build systems.
JavaScript is used to interact and communicate.
Your browsers runs JS to communicate with:
btn.addEventListener("click", sayHello);
fetch("https://api.nasa.gov").then(...)
Whereas in Java, programs often have a well-defind specification (behavior), JS has to deal with uncertainty (weird users, unavailable servers, no internet connection, etc.)
The JS programs we've been writing are naturally asynchronous
We pass functions as arguments to other functions so that we can “call back later” once we know something we expect occurred.
Our goal is to assign tasks to events, and responsibly handle uncertainty.
Luckily, we have a handy toolset of functions:
addEventListener
, setTimeout/setInterval
, fetch
, etc.)and developer tools
Remember - as a human, you already are skilled at dealing with uncertainty! Your task is just to teach JS how it needs to handle events.
Our first example of handling "uncertain" behavior was addEventListener("click", openBox)
On Friday, we introduced setTimeout
and setInterval
.
Later this week, we will introduce fetch
and Promises.
First-class function: Treated just like any other variable type
Can store as variables, pass as arguments to functions, return as values from functions
function callbackFn(params) {
...
}
let callbackFn = function(params) {
...
};
let callbackFn = (params) => {
...
};
JS
Regardless of how we create these, we use them the same way.
let result = callbackFn(params);
JS
btn.addEventListener("click", callbackFn);
btn.addEventListener("click", function() {
...
});
btn.addEventListener("click", () => {
...
});
JS
setTimeout(callbackFn, 2000);
setTimeout(function() {
...
}, 2000);
setTimeout(() => {
...
}, 2000);
JS
A slight modification of the callback example from the reading
Is this a higher-order function?
function askQ(qText) {
let answer = prompt(qText);
if (answer === "yes") {
console.log("You answered yes!");
} else if (answer === "no") {
console.log("You answered no!");
} else {
console.log("I have no idea what " + qText + " means.");
}
}
JS
Demo: (starter)
Is this a higher-order function?
function askQ(qText, yesFn, noFn, handleError) {
let answer = prompt(qText);
if (answer === "yes") {
yesFn();
} else if (answer === "no") {
noFn();
} else {
handleError(answer);
}
}
JS
function yesFn() {
console.log("You answered yes!");
}
function noFn() {
console.log("You answered no!");
}
function handleError(answer) {
console.log("I have no idea what " +
answer + " means.");
}
JS
function askQ(qText, yesFn, noFn, handleError) {
let answer = prompt(qText);
if (answer === "yes") {
// yesFn();
yesFn2();
} else if (answer === "no") {
// noFn();
noFn2();
} else {
handleError(answer);
}
}
JS
function yesFn2() {
id("output").textContent = "You answered yes!";
}
function noFn() {
id("output").textContent = "You answered no!";
}
function handleError(answer) {
console.log("I have no idea what " + answer + " means. Try again");
processQuestion();
}
JS
Callbacks are a very powerful feature in event-driven programming.
Why do you think it's useful to have the ability in the JavaScript language to pass callback
functions as arguments to other functions like addEventListener
and setTimeout
in JS?
Used to delay or set intervals for executing functions
(function() {
console.log("Foo 1");
window.addEventListener("load", init);
function init() {
setTimeout(testFunction, 1000);
console.log("Foo 2");
}
function testFunction() {
console.log("Foo 3");
}
console.log("Foo 4");
})();
precheck-q4.js
method | description |
---|---|
setTimeout(responseFn, delayMS) | Arranges to call given function after given delayMS, returns timer id |
setInterval(responseFn, delayMS) | Arranges to call function repeatedly every delayMS ms, returns timer id |
clearTimeout(timerID)
clearInterval(timerID) |
Stops the given timer |
Both setTimeout
and setInterval
return an ID
representing the timer
This id is used as a "key" to tell the window to stop the specific timer.
Practice: timers-practice.html
setTimeout
Example<button id="demo-btn">Click me!</button>
<span id="output-text"></span>
HTML
function init() {
id("demo-btn").addEventListener("click", delayedMessage);
}
function delayedMessage() {
id("output-text").textContent = "Wait for it...";
setTimeout(sayHello, 500);
}
function sayHello() { // called when the timer goes off
id("output-text").textContent = "Hello!";
}
JS
output (full example page)
setInterval
Example<button id="demo-btn">Click me!</button>
<span id="output-text"></span>
HTML
let timerId = null; // stores ID of interval timer
function repeatedMessage() {
timerId = setInterval(sayHello, 1000);
}
function sayHello() {
id("output-text").textContent += "Hello!";
}
JS
output (full example page)
clearInterval
<button id="toggle-btn">Start/Stop<button>
HTML
let timerId = null; // stores ID of interval timer
function init() {
id("toggle-btn").addEventListener("click", toggleMessageInterval);
}
// 1. What does this function do?
function toggleMessageInterval() {
if (timerId === null) {
timerId = setInterval(sayHello, 1000);
} else {
clearInterval(timerId);
timerId = null; // 2. Why is this line important?
// 3. What happens if you swap the two lines above?
}
}
function sayHello() {
id("output-text").textContent += "Hello!";
}
JS
output (full example page)
Starter code: typing-simulator-starter.html
Running solution: typing-simulator.html
What other features can you think to add? A dropdown with speed? Multiple timers?
When you want to call a function after a specified delay in time, use
setTimeout
.
When you want to call a function repeatedly every X seconds, use
setInterval
(though you can also use setTimeout
recursively!)
For both types of timers, you'll need a variable with the timer id (returned
by both functions) to pass to clearTimeout
/clearInterval
when
you want to stop the delay/interval.