Zach's Mugspideyclick logo

GitHub

GitLab

Linkedin

Instagram

Youtube

SoundCloud

Email

Node JS

Cheat Sheet

http://overapi.com/javascript

Handy JavaScript Functions

https://gitlab.com/snippets/1820469

Recursive Functions + For Loops

https://stackoverflow.com/questions/6419128/javascript-for-loop-variable-and-recursion

Cache the length of the array so you would have the following:

function recurse(node) {
    for(var i = 0, count = node.children.length; i < count; i++) {
        recurse(node.children[i]);
    }
}

You should always cache especially when you're dealing with HTMLCollections.

Just another note from me: the "count" variable needs to be unique from other "count" variables used on other loops within the same function.

Sleep Function

https://flaviocopes.com/javascript-sleep/

Create the sleep function:

const sleep = (milliseconds) => {
  return new Promise(resolve => setTimeout(resolve, milliseconds))
}

Use the sleep function:

sleep(500).then(() => {
  //do stuff
})

Or use it in an async function:

const doSomething = async () => {
  await sleep(2000)
  //do stuff
}

doSomething()

Remember that due to how JavaScript works (read more about the event loop), this does not pause the entire program execution like it might happen in other languages, but instead only your function sleeps.