Global scope
setInterval
setInterval()
repeatedly calls a function or executes a code snippet, with a fixed time delay between each call.
The example below will call function
every 1000 milliseconds (1 second).
1 | let timerId = setInterval(function, 1000); |
clearInterval
clearInterval()
cancels a timed, repeating action which was previously established by a call to setInterval()
.
1 | clearInterval(timerId) |
EventTarget
EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them. Element, Document, and Window are the most common event targets.
addEventListener
addEventListener()
sets up a function that will be called whenever the specified event is delivered to the target, here it will fire when the DOM is loaded. This is the same as jQuerys $(document).ready(function() { });
.
The syntax is [TARGET].addEventListener('event', function)
, common targets include
- element
- document
- window
Some common events
- onclick
- keydown
- keypress
- keyup
- click
- auxclick
- contextmenu
- mousedown
- mouseenter
- mouseleave
- mouseover
- mouseout
1 | document.addEventListener('DOMContentLoaded', () => { |
removeEventListener
Removes the event listner.
1 | document.removeEventListener('keyup', control); |
Document
The Document interface represents any web page loaded in the browser and serves as an entry point into the web page’s content, which is the DOM tree.
querySelector
querySelector()
returns the first Element within the document that matches the specified selector
1 | const grid = document.querySelector('.grid'); |
querySelectorAll
querySelectorAll()
returns a static node list representing a list of the document’s elements that match the specified group of selectors.
For the ids like #score
we can also use document.getElementById('#score')
1 | let squares = document.querySelectorAll('.grid div'); |
Element
classList
classList
is used to access an element’s list of classes as a space-delimited string.
classList.add
then appends to it.
1 | div.classList.add("foo", "bar", "baz"); |
classList.remove
then appends to it.
1 | div.classList.remove("foo"); |
classList.contains
checks if the class exists then returns true if it does, else returns false.
1 | div.classList.contains("foo"); |
appendChild & createElement
appendChild
allows us to append elements to an existing element, here the element that to be appended is created with createElement
1 | const grid = document.querySelector('.grid'); |
- https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild
- https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement
KeyboardEvent
keyCode (DEPRICATED)
1 | function control(e) { |