Categories
JavaScript Answers

How to prevent “The play() request was interrupted by a call to pause()” error with JavaScript?

Sometimes, we want to prevent "The play() request was interrupted by a call to pause()" error with JavaScript.

In this article, we’ll look at how to prevent "The play() request was interrupted by a call to pause()" error with JavaScript.

How to prevent "The play() request was interrupted by a call to pause()" error with JavaScript?

To prevent "The play() request was interrupted by a call to pause()" error with JavaScript, we can check if the play is called only when the video is paused.

For instance, we write

let isPlaying = true;

video.onplaying = () => {
  isPlaying = true;
};

video.onpause = () => {
  isPlaying = false;
};

const playVid = () => {
  if (video.paused && !isPlaying) {
    return video.play();
  }
};

const pauseVid = () => {
  if (!video.paused && isPlaying) {
    video.pause();
  }
};

to set isPlaying to true when the video is playing.

And we set it to false when we pause the video.

We call video.play when the video is paused and isPlaying is false.

And we call video.pause when the video isn’t paused and isPlaying is true.

Conclusion

To prevent "The play() request was interrupted by a call to pause()" error with JavaScript, we can check if the play is called only when the video is paused.

Categories
JavaScript Answers

How to convert JavaScript iterable to array?

Sometimes, we want to convert JavaScript iterable to array.

In this article, we’ll look at how to convert JavaScript iterable to array.

How to convert JavaScript iterable to array?

To convert JavaScript iterable to array, we can use the spread operator.

For instance, we write

const map = new Map([
  [1, "one"],
  [2, "two"],
]);

const newArr = [...map];

to convert the map to the newArr array.

newArr is an array with arrays of keys and values in the map.

Conclusion

To convert JavaScript iterable to array, we can use the spread operator.

Categories
JavaScript Answers

How to use JavaScript includes() in a case insensitive manner?

Sometimes, we want to use JavaScript includes() in a case insensitive manner.

In this article, we’ll look at how to use JavaScript includes() in a case insensitive manner.

How to use JavaScript includes() in a case insensitive manner?

To use JavaScript includes() in a case insensitive manner, we can convert all strings to the same case.

For instance, we write

const arr = ["foo", "bar", "bar"];
const lookup = "bAr";
console.log(arr.some((x) => x.toLowerCase() === lookup.toLowerCase()));

to call arr.some with a callback that converts the lookup string and the arr entry string x to lower case with toLowerCase.

And then we compare them to see if they’re equal in the some callback.

some returns true if any value in arr matches the condition we return in the callback.

Conclusion

To use JavaScript includes() in a case insensitive manner, we can convert all strings to the same case.

Categories
JavaScript Answers

How to get the sha1 hash of a string in Node.js?

To get the sha1 hash of a string in Node.js, we can use the crypto module.

For instance, we write

const crypto = require("crypto");
const shasum = crypto.createHash("sha1");
shasum.update("foo");
const digest = shasum.digest("hex");

to call crypto.createHash to create the shasum hash object.

Then we call shasum.update to update the hash with the 'foo' string.

And then we return the hash digest with digest.

Categories
JavaScript Answers

How to encode HTML entities in JavaScript?

To encode HTML entities in JavaScript, we can use the string replace and charCodeAt methods.

For instance, we write

const encodedStr = rawStr.replace(/[\u00A0-\u9999<>\&]/g, (i) => {
  return "&#" + i.charCodeAt(0) + ";";
});

to call replace with a regex pattern that we want to look for in rawStr and a callback does the replacement.

In the callback, we get each result and replace them with the HTML entity with the character code that we get with i.charCodeAt(0).

We prepend "&#" and append ';' to the character code to form the entity.

Conclusion

To encode HTML entities in JavaScript, we can use the string replace and charCodeAt methods.