Categories
JavaScript Answers

How to convert time interval given in seconds into more human readable form with JavaScript?

Sometimes, we want to convert time interval given in seconds into more human readable form with JavaScript.

In this article, we’ll look at how to convert time interval given in seconds into more human readable form with JavaScript.

How to convert time interval given in seconds into more human readable form with JavaScript?

To convert time interval given in seconds into more human readable form with JavaScript, we can use the moment duration humanize method.

For instance, we write

const duration = moment.duration(31536000);
console.log(duration.humanize());

to create a duration object by calling moment.duration with a timestamp in milliseconds.

Then we call duration.humanize to return a human readable string with the duration.

Conclusion

To convert time interval given in seconds into more human readable form with JavaScript, we can use the moment duration humanize method.

Categories
JavaScript Answers

How to unzip files with JavaScript?

Sometimes, we want to unzip files with JavaScript.

In this article, we’ll look at how to unzip files with JavaScript.

How to unzip files with JavaScript?

To unzip files with JavaScript, we use the JSZip library.

To install it, we run

npm install jszip

Then we use it by writing

const JSZip = require("jszip");
const zip = new JSZip();
zip.load(file);
zip.files["doc.xml"].asText()

to create the zip JSZip object.

Then we call zip.load to load the file that we get from the file input.

Next, we get the doc.xml file with zip.files["doc.xml"].

And we read it as text with asText.

Conclusion

To unzip files with JavaScript, we use the JSZip library.

Categories
JavaScript Answers

How to redraw or scale Google chart on window resize with JavaScript?

Sometimes, we want to redraw or scale Google chart on window resize with JavaScript.

In this article, we’ll look at how to redraw or scale Google chart on window resize with JavaScript.

How to redraw or scale Google chart on window resize with JavaScript?

To redraw or scale Google chart on window resize with JavaScript, we can redraw the chart when we resize the screen.

For instance, we write

const resize = () => {
  const chart = new google.visualization.LineChart(
    document.getElementById("chart_div")
  );
  chart.draw(data, options);
};

window.onload = resize;
window.onresize = resize;

to create the resize function.

In it, we create a LineChart object with the container element of the chart.

And then we call draw to draw the line chart.

Then we set window.onload and window.onresize to resize to call it when the page loads and when we resize the page respectively.

Conclusion

To redraw or scale Google chart on window resize with JavaScript, we can redraw the chart when we resize the screen.

Categories
React Answers

How to render React components with promises inside the render method?

Sometimes, we want to render React components with promises inside the render method.

In this article, we’ll look at how to render React components with promises inside the render method.

How to render React components with promises inside the render method?

To render React components with promises inside the render method, we use the react-promise library.

To install it, we run

npm i react-promise

Then we use it by writing

import usePromise from "react-promise";

const ExampleWithAsync = (props) => {
  const { value, loading } = usePromise(prom);
  if (loading) {
    return null;
  }
  return <div>{value}</div>;
};

We call the usePromise hook with the prom promise.

And we get the resolve value of prom from value.

If loading is true, then prom is being run.

Conclusion

To render React components with promises inside the render method, we use the react-promise library.

Categories
Vue Answers

How to access to data from methods with Vue.js?

Sometimes, we want to access to data from methods with Vue.js.

In this article, we’ll look at how to access to data from methods with Vue.js.

How to access to data from methods with Vue.js?

To access to data from methods with Vue.js, we can use reactive properties.

For instance, we write

export default {
  data() {
    return {
      questions: [],
      sendButtonDisable: false,
    };
  },

  methods: {
    postQuestionsContent() {
      this.sendButtonDisable = true;
    },
  },
};

to add the sendButtonDisable reactive property and set its initial value to false in the data method.

Then we add the postQuestionsContent method that sets the sendButtonDisable reactive property to true.

Conclusion

To access to data from methods with Vue.js, we can use reactive properties.