Categories
JavaScript Answers

How to use setInterval and clearInterval with JavaScript?

Sometimes, we want to use setInterval and clearInterval with JavaScript.

In this article, we’ll look at how to use setInterval and clearInterval with JavaScript.

How to use setInterval and clearInterval with JavaScript?

To use setInterval and clearInterval with JavaScript, we call clearInterval to clear the repeating timer with the timer ID which is returned by setInterval.

For instance, we write

const handle = setInterval(drawAll, 20);

//...

clearInterval(handle);

to call setInterval with the drawAll function and 20 milliseconds to call drawAll every 20 milliseconds.

And then we call clearInterval with the handle to clear the timer returned by setInterval.

Conclusion

To use setInterval and clearInterval with JavaScript, we call clearInterval to clear the repeating timer with the timer ID which is returned by setInterval.

Categories
JavaScript Answers

How to fix JavaScript date.getYear() returning 111 in 2011?

Sometimes, we want to fix JavaScript date.getYear() returning 111 in 2011.

In this article, we’ll look at how to fix JavaScript date.getYear() returning 111 in 2011.

How to fix JavaScript date.getYear() returning 111 in 2011?

To fix JavaScript date.getYear() returning 111 in 2011, we replace getYear with getFullYear.

For instance, we write

const y = new Date().getFullYear();

to get the 4 digit year of the current date with getFullYear.

Conclusion

To fix JavaScript date.getYear() returning 111 in 2011, we replace getYear with getFullYear.

Categories
JavaScript Answers

How to return index of greatest value in an array with JavaScript?

Sometimes, we want to return index of greatest value in an array with JavaScript.

In this article, we’ll look at how to return index of greatest value in an array with JavaScript.

How to return index of greatest value in an array with JavaScript?

To return index of greatest value in an array with JavaScript, we can use the Math.max method and the array indexOf method.

For instance, we write

const i = arr.indexOf(Math.max(...arr));

to call Math.max with the arguments of arr spread as the arguments of max.

Then we call indexOf on the max number in arr returned by Math.max to get the index of the greatest number in arr.

Conclusion

To return index of greatest value in an array with JavaScript, we can use the Math.max method and the array indexOf method.

Categories
JavaScript Answers

How to check image width and height before upload with JavaScript?

Sometimes, we want to check image width and height before upload with JavaScript.

In this article, we’ll look at how to check image width and height before upload with JavaScript.

How to check image width and height before upload with JavaScript?

To check image width and height before upload with JavaScript, we can get the dimensions by assigning the image data string to the src property of an img element.

For instance, we write

const reader = new FileReader();

reader.readAsDataURL(fileUpload.files[0]);
reader.onload = (e) => {
  const image = new Image();
  image.src = e.target.result;
  image.onload = (e) => {
    const height = e.target.height;
    const width = e.target.width;
    if (height > 100 || width > 100) {
      alert("Height and Width must not exceed 100px.");
      return false;
    }
    alert("Uploaded image has valid Height and Width.");
    return true;
  };
};

to create a FileReader object in the file input change event handler.

Then we call readAsDataURL with fileUpload.files[0] to read the fileUpload.files[0] selected file into a base64 string.

Then we create an img element with Image and set the base64 image URL string as the src attribute value with

image.src = e.target.result;

And then we get the width and height from e.target with

const height = e.target.height;
const width = e.target.width;

And then we can check the width and height after that.

Conclusion

To check image width and height before upload with JavaScript, we can get the dimensions by assigning the image data string to the src property of an img element.

Categories
JavaScript Answers

How to split string with newline (‘\n’) in Node.js?

Sometimes, we want to split string with newline (‘\n’) in Node.js.

In this article, we’ll look at how to split string with newline (‘\n’) in Node.js.

How to split string with newline (‘\n’) in Node.js?

To split string with newline (‘\n’) in Node.js, we can use the string split method.

For instance, we write

const arr = "a\nb\r\nc".split(/\r?\n/);

to call "a\nb\r\nc".split with the regex /\r?\n/ to split the string by \r\n or \n into separate strings.

Conclusion

To split string with newline (‘\n’) in Node.js, we can use the string split method.