Asynchronous Javascript: SetTimeout and SetInterval

SetTimeout and SetInterval provide ways to schedule tasks to run at a future point in time. setTimeout allows you to schedule a task after a given interval. setInterval lets you run a task periodically with a given interval between runs. SetTimeout setTimeout takes two parameters: A string representing code to run and a number representing the time interval in milliseconds to wait before executing the code. In the following example, the browser will wait two seconds before executing the anonymous function and presenting the alert message. let myGreeting = setTimeout(function() { alert(‘Hello, Mr. Universe!’); }, 2000) We’re not required to

Asynchronous Javascript: Introduction and Synchronous Code

When we write Javascript we normally do it synchronously, every instruction is executed one at a time in the order they appear in the script. The script will finish once the last instruction is executed. In the example below, we log three items to the console. console.log(‘1’); console.log(‘2’); console.log(‘3′); We will always get the same result: 1 2 3. This works great for small pieces of code or scripts that don’t produce output on the browser. But sooner rather than later you will want to work on larger scripts and you will find one of the first issues with Javascript:

Prototypal Inheritance and Classes

If you’re working with modern evergreen browsers, classes are the preferred way to create reusable code. They work in all modern browsers according to caniuse.com out of the box. Prototypal inheritance will work if you need to support older browsers. Prototypal Inheritance Before ES6 we created reusable content using functions to create objects and attaching the methods to the object’s prototype. This way every object that inherits from the object will also inherit the methods attached to the prototype. In the example below the Person has the following properties: first, last, age, gender, and interests. all these properties are required