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.

Categories
HTML

How to set value to currency in number input with HTML?

Sometimes, we want to set value to currency in number input with HTML.

In this article, we’ll look at how to set value to currency in number input with HTML.

How to set value to currency in number input with HTML?

To set value to currency in number input with HTML, we set the step attribute.

For instance, we write

<input type="number" min="0.01" step="0.01" max="2500" value="25.67" />

to set the step attribute to 0.01 to let us enter numbers with 2 decimal places.

Conclusion

To set value to currency in number input with HTML, we set the step attribute.

Categories
JavaScript Answers

How to load textures in THREE.js and JavaScript?

Sometimes, we want to load textures in THREE.js and JavaScript.

In this article, we’ll look at how to load textures in THREE.js and JavaScript.

How to load textures in THREE.js and JavaScript?

To load textures in THREE.js and JavaScript, we call loadTexture with a callback that’s called when the texture is loaded.

For instance, we write

const texture = THREE.ImageUtils.loadTexture("crate.gif", {}, () => {
  renderer.render(scene);
});

to call loadTexture with the path to the texture file.

And we call it with a callback that’s called after the texture is loaded as the 3rd argument.

Conclusion

To load textures in THREE.js and JavaScript, we call loadTexture with a callback that’s called when the texture is loaded.

Categories
TypeScript Answers

How to break a forEach loop in TypeScript?

Sometimes, we want to break a forEach loop in TypeScript.

In this article, we’ll look at how to break a forEach loop in TypeScript.

How to break forEach loop in TypeScript?

To break a forEach loop in TypeScript, we replace it with a for-of loop.

For instance, we write

for (const a of ratings) {
  if (somethingWrong) {
    break;
  }
  //...
}

to loop through the ratings array.

And if somethingWrong is true, we use break to stop the loop.

Conclusion

To break a forEach loop in TypeScript, we replace it with a for-of loop.