Categories
JavaScript Answers

How to create a sleep or delay in Node.js that is not blocking with JavaScript?

Sometimes, we want to create a sleep or delay in Node.js that is not blocking with JavaScript.

In this article, we’ll look at how to create a sleep or delay in Node.js that is not blocking with JavaScript.

How to create a sleep or delay in Node.js that is not blocking with JavaScript?

To create a sleep or delay in Node.js that is not blocking with JavaScript, we can use a promise.

For instance, we write

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

const test = async () => {
  await sleep(1000);
  console.log("one second has elapsed");
};

to define the sleep function that returns a promise that calls setTimeout which calls resolve after millis milliseconds.

Then we call sleep in test with 1000 to pause for 1000 milliseconds.

Conclusion

To create a sleep or delay in Node.js that is not blocking with JavaScript, we can use a promise.

Categories
JavaScript Answers

How to define a JavaScript regular expression that validates a password that has special characters?

Sometimes, we want to define a JavaScript regular expression that validates a password that has special characters.

In this article, we’ll look at how to define a JavaScript regular expression that validates a password that has special characters.

How to define a JavaScript regular expression that validates a password that has special characters?

To define a JavaScript regular expression that validates a password that has special characters, we can use the (?=.*[0-9]) and (?=.*[!@#$%^&*]) patterns.

For instance, we write

const regularExpression =
  /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{6,16}$/;

to define the regularExpression regex that has both groups.

(?=.*[0-9]) checks that the string has digits.

And (?=.*[!@#$%^&*]) checks that a string has at least 1 special character.

Conclusion

To define a JavaScript regular expression that validates a password that has special characters, we can use the (?=.*[0-9]) and (?=.*[!@#$%^&*]) patterns.

Categories
JavaScript Answers

How to get all checked checkboxes with JavaScript?

Sometimes, we want to get all checked checkboxes with JavaScript.

In this article, we’ll look at how to get all checked checkboxes with JavaScript.

How to get all checked checkboxes with JavaScript?

To get all checked checkboxes with JavaScript, we use querySelectorAll.

For instance, we write

const checkedBoxes = document.querySelectorAll(
  "input[name=mycheckboxes]:checked"
);

to call querySelectorAll with "input[name=mycheckboxes]:checked" to select all checkboxes with the name attribute set to mycheckboxes.

And we use :checked to select the checked ones with the attribute.

Conclusion

To get all checked checkboxes with JavaScript, we use querySelectorAll.

Categories
JavaScript Answers

How to create a HTML 5 video or audio playlist with JavaScript?

Sometimes, we want to create a HTML 5 video or audio playlist with JavaScript.

In this article, we’ll look at how to create a HTML 5 video or audio playlist with JavaScript.

How to create a HTML 5 video or audio playlist with JavaScript?

To create a HTML 5 video or audio playlist with JavaScript, we can load the next video after the current one ends.

For instance, we write

<video
  id="videoPlayer"
  src="path/of/current/video.mp4"
  autoplay
  autobuffer
  controls
/>

to add a video element.

Then we write

const nextVideo = "path/of/next/video.mp4";
const videoPlayer = document.getElementById("videoPlayer");
videoPlayer.onended = () => {
  videoPlayer.src = nextVideo;
};

to select the video element with getElementById.

And then we set its onended property to a function that updates the src attribute to the path of the next video to play.

Conclusion

To create a HTML 5 video or audio playlist with JavaScript, we can load the next video after the current one ends.

Categories
JavaScript Answers

How to execute a method passed as parameter to function with JavaScript?

Sometimes, we want to execute a method passed as parameter to function with JavaScript.

In this article, we’ll look at how to execute a method passed as parameter to function with JavaScript.

How to execute a method passed as parameter to function with JavaScript?

To execute a method passed as parameter to function with JavaScript, we just call the function.

For instance, we write

const myFunction = (param1, callbackFunction) => {
  //...
  callbackFunction();
};

to call callbackFunction in myFunction.

Conclusion

To execute a method passed as parameter to function with JavaScript, we just call the function.