Categories
JavaScript Answers

How to fix incorrect date shown in new Date() in JavaScript?

Sometimes, we want to fix incorrect date shown in new Date() in JavaScript.

In this article, we’ll look at how to fix incorrect date shown in new Date() in JavaScript.

How to fix incorrect date shown in new Date() in JavaScript?

To fix incorrect date shown in new Date() in JavaScript, we should include the time in the date string when we use it as an argument for Date.

For instance, we write:

const d = new Date('2022-09-05 00:00')
console.log(d)

to create date d with '2022-09-05 00:00' as the argument of the Date constructor.

As a result, d is Mon Sep 05 2022 00:00:00 GMT-0700 (Pacific Daylight Time).

Conclusion

To fix incorrect date shown in new Date() in JavaScript, we should include the time in the date string when we use it as an argument for Date.

Categories
JavaScript Answers

How to use setTimeout in an async function with JavaScript?

Sometimes, we want to use setTimeout in an async function with JavaScript.

In this article, we’ll look at how to use setTimeout in an async function with JavaScript.

How to use setTimeout in an async function with JavaScript?

To use setTimeout in an async function with JavaScript, we can create a promise that calls setTimeout.

For instance, we write:

const wait = delay => new Promise(resolve => setTimeout(resolve, delay));

const f = async () => {
  console.log(1)
  await wait(2000);
  console.log(2)
}
f()

to define the wait function that returns a promise which calls setTimeout with resolve as the callback and after delay milliseconds.

Once resolve is called, the promise finishes running.

Next, we define the function f that calls wait between the 2 console logs.

Therefore, we see 2 logged after 2 seconds after 1.

Conclusion

To use setTimeout in an async function with JavaScript, we can create a promise that calls setTimeout.

Categories
JavaScript Answers

How to call a JavaScript function when iframe finished loading?

Sometimes, we want to call a JavaScript function when iframe finished loading.

In this article, we’ll look at how to call a JavaScript function when iframe finished loading.

How to call a JavaScript function when iframe finished loading?

To call a JavaScript function when iframe finished loading, we can add an event listener for the load event.

For instance, we write:

<iframe src='https://example.com'></iframe>

to add an iframe.

Then we write:

const iframe = document.querySelector('iframe')
iframe.onload = () => {
  console.log('loaded')
}

to select the iframe with querySelector.

Then we set the iframe.onload property to a function that logs 'loaded'.

As a result, we see 'loaded' logged when the iframe is loaded.

Conclusion

To call a JavaScript function when iframe finished loading, we can add an event listener for the load event.

Categories
JavaScript Answers

How to iterate through a table rows and get the cell values using JavaScript?

Sometimes, we want to iterate through a table rows and get the cell values using JavaScript.

In this article, we’ll look at how to iterate through a table rows and get the cell values using JavaScript.

How to iterate through a table rows and get the cell values using JavaScript?

To iterate through a table rows and get the cell values using JavaScript, we can use the row property.

For instance, we write:

<table>
  <thead>
    <tr>
      <th>Column 1</th>
      <th>Column 2</th>
      <th>Column 3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>A1</td>
      <td>A2</td>
      <td>A3</td>
    </tr>
    <tr>
      <td>B1</td>
      <td>B2</td>
      <td>B3</td>
    </tr>
    <tr>
      <td>C1</td>
      <td>C2</td>
      <td>C3</td>
    </tr>
  </tbody>
</table>

to add a table.

Then we write:

const table = document.querySelector('table')
for (const r of table.rows) {
  for (const c of r.cells) {
    console.log(c.innerHTML)
  }
}

to select the table with querySelector.

Then we loop through the table.rows property which is an object with the tr elements.

Then we loop through the cells which has the th and td elements.

And then we get the innerHTML value to get its contents.

Conclusion

To iterate through a table rows and get the cell values using JavaScript, we can use the row property.

Categories
JavaScript Answers React React Answers

How to access canvas context in React and JavaScript?

Sometimes, we want to access canvas context in React and JavaScript.

In this article, we’ll look at how to access canvas context in React and JavaScript.

How to access canvas context in React and JavaScript?

To access canvas context in React and JavaScript, we can assign a ref to the canvas and call getContext on the ref’s value.

For instance, we write:

import React from "react";

export default function App() {
  const ref = React.useRef();
  console.log(ref.current.getContext("2d"));

  return (
    <>
      <canvas ref={ref} />
    </>
  );
}

to call useRef to create a ref.

Then we get the context by calling ref.current.getContext with '2d' to get the context.

Conclusion

To access canvas context in React and JavaScript, we can assign a ref to the canvas and call getContext on the ref’s value.