Categories
JavaScript Answers

How to watch for array changes with JavaScript?

Sometimes, we want to watch for array changes with JavaScript.

In this article, we’ll look at how to watch for array changes with JavaScript.

How to watch for array changes with JavaScript?

To watch for array changes with JavaScript, we use the underscore-observe library.

We use it by installing Lodash or Underscore.

Then we add contents of the https://github.com/mennovanslooten/underscore-observe/blob/master/underscore-observe.js file into our code.

Then we watch an array for changes by writing

const a = ["zero", "one", "two", "trhee"];

_.observe(a, () => {
  alert("something happened");
});

We call observe with array a and a callback that runs when a changes.

Conclusion

To watch for array changes with JavaScript, we use the underscore-observe library.

Categories
JavaScript Answers

How to check time difference in JavaScript?

Sometimes, we want to check time difference in JavaScript.

In this article, we’ll look at how to check time difference in JavaScript.

How to check time difference in JavaScript?

To check time difference in JavaScript, we can use the date getTime method.

For instance, we write

const date1 = new Date("08/05/2022 23:41:20");
const date2 = new Date("08/06/2022 02:56:32");

const diff = date2.getTime() - date1.getTime();

let msec = diff;
const hh = Math.floor(msec / 1000 / 60 / 60);
msec -= hh * 1000 * 60 * 60;
const mm = Math.floor(msec / 1000 / 60);
msec -= mm * 1000 * 60;
const ss = Math.floor(msec / 1000);
msec -= ss * 1000;

to call getTime to get the timestamp of the dates in milliseconds.

Then we get the difference of the timestamps.

Next, we get the hours with Math.floor(msec / 1000 / 60 / 60).

Then we subtract that from msec and we use the new msec value to get the minutes difference with Math.floor(msec / 1000 / 60).

Likewise, we use the same steps to get the seconds difference and we get the seconds difference with Math.floor(msec / 1000).

Conclusion

To check time difference in JavaScript, we can use the date getTime method.

Categories
JavaScript Answers

How to get document height and width with JavaScript?

Sometimes, we want to get document height and width with JavaScript.

In this article, we’ll look at how to get document height and width with JavaScript.

How to get document height and width with JavaScript?

To get document height and width with JavaScript, we can use the clientWidth and clientHeight properties.

For instance, we write

const height = document.body.clientHeight;
const width = document.body.clientWidth;

to get the width and height of the body element with clientWidth and clientHeight respectively.

We use document.body to get the body element.

Conclusion

To get document height and width with JavaScript, we can use the clientWidth and clientHeight properties.

Categories
JavaScript Answers

How to save JSON to local text file with JavaScript?

Sometimes, we want to save JSON to local text file with JavaScript.

In this article, we’ll look at how to save JSON to local text file with JavaScript.

How to save JSON to local text file with JavaScript?

To save JSON to local text file with JavaScript. we can create a blob from the text.

For instance, we write

const a = document.createElement("a");
const file = new Blob([content], { type: "text/plain" });
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();

to create a link with createElement.

Then we create a blob with the content string with the Blob constructor.

We set its MIME type to 'text/plain'.

And then we create an object URL from the blob with createObjectURL.

Next, we set the download property to a string with the name of the downloaded file.

Then we call click to download the content text into a file.

Conclusion

To save JSON to local text file with JavaScript. we can create a blob from the text.

Categories
JavaScript Answers

How to convert a Buffer into a ReadableStream in Node.js?

Sometimes, we want to convert a Buffer into a ReadableStream in Node.js.

In this article, we’ll look at how to convert a Buffer into a ReadableStream in Node.js.

How to convert a Buffer into a ReadableStream in Node.js?

To convert a Buffer into a ReadableStream in Node.js, we call the Readable.from method.

For instance, we write

const { Readable } = require("stream");

const stream = Readable.from(myBuffer.toString());

to call Readable.from with the myBuffer buffer converted to a string to convert myBuffer to a readable stream.

Conclusion

To convert a Buffer into a ReadableStream in Node.js, we call the Readable.from method.