Categories
JavaScript Answers

How to create a JavaScript callback for knowing when an image is loaded?

Sometimes, we want to create a JavaScript callback for knowing when an image is loaded.

In this article, we’ll look at how to create a JavaScript callback for knowing when an image is loaded.

How to create a JavaScript callback for knowing when an image is loaded?

To create a JavaScript callback for knowing when an image is loaded, we can use the onload method.

For instance, we write

const logo = document.getElementById("sologo");

logo.onload = () => {
  console.log("The image has loaded!");
};

logo.src = "https://picsum.photos/200/300";

to get the img element with getElementById.

Then we set logo.onload to a function that runs when the image is loaded.

Finally, we set logo.src to the image URL to load the image.

Conclusion

To create a JavaScript callback for knowing when an image is loaded, we can use the onload method.

Categories
JavaScript Answers

How to get seconds since epoch in JavaScript?

Sometimes, we want to get seconds since epoch in JavaScript.

In this article, we’ll look at how to get seconds since epoch in JavaScript.

How to get seconds since epoch in JavaScript?

To get seconds since epoch in JavaScript, we get the timestamp of the datetime in milliseconds with getTime.

And then we divide that by 1000 to get the number of seconds since epoch.

For instance, we write

const d = new Date();
const seconds = d.getTime() / 1000;

to create the date d.

And then we call d.getTime to return the timestamp in milliseconds.

Then we divide that by 1000 to get the number of seconds since epoch.

Conclusion

To get seconds since epoch in JavaScript, we get the timestamp of the datetime in milliseconds with getTime.

And then we divide that by 1000 to get the number of seconds since epoch.

Categories
JavaScript Answers

How to subtract minutes from a date in JavaScript?

Sometimes, we want to subtract minutes from a date in JavaScript.

In this article, we’ll look at how to subtract minutes from a date in JavaScript.

How to subtract minutes from a date in JavaScript?

To subtract minutes from a date in JavaScript, we can subtract the timestamp in milliseconds from the current date.

For instance, we write

const MS_PER_MINUTE = 60000;
const myStartDate = new Date(myEndDateTime - durationInMinutes * MS_PER_MINUTE);

to subtract the myEndDateTime timestamp in milliseconds by the durationInMinutes * MS_PER_MINUTE milliseconds, which is the durationInMinutes converted to milliseconds.

Then we pass that into the Date constructor to create a new date object.

Conclusion

To subtract minutes from a date in JavaScript, we can subtract the timestamp in milliseconds from the current date.

Categories
JavaScript Answers

How to append to an object with JavaScript?

Sometimes, we want to append to an object with JavaScript.

in this article, we’ll look at how to append to an object with JavaScript.

How to append to an object with JavaScript?

To append to an object with JavaScript, we can use the spread operator.

For instance, we write

let alerts = {
  1: { app: "helloworld", message: "message" },
  2: { app: "helloagain", message: "another message" },
};

alerts = {
  ...alerts,
  alertNo: 2,
};

to spread the existing properties of alerts into a new object with ....

And then we add the alertNo property to it.

Then we assign the new object back to alerts.

Conclusion

To append to an object with JavaScript, we can use the spread operator.

Categories
JavaScript Answers

How to traverse all the nodes of a JSON object tree with JavaScript?

Sometimes, we want to traverse all the nodes of a JSON object tree with JavaScript.

In this article, we’ll look at how to traverse all the nodes of a JSON object tree with JavaScript.

How to traverse all the nodes of a JSON object tree with JavaScript?

To traverse all the nodes of a JSON object tree with JavaScript, we can use the Object.entries method.

For instance, we write

const traverse = (jsonObj) => {
  if (jsonObj !== null && typeof jsonObj == "object") {
    Object.entries(jsonObj).forEach(([key, value]) => {
      traverse(value);
    });
  } else {
    console.log(jsonObj);
  }
};

to create the traverse function.

In it, we loop through the key-value pairs with the Object.entries method.

If jsonObj is an object, then we call traverse with its value in the forEach callback to traverse the level below the current level.

Otherwise, we log the jsonObj value.

Conclusion

To traverse all the nodes of a JSON object tree with JavaScript, we can use the Object.entries method.