Categories
JavaScript Answers

How to change cursor when loading page in JavaScript?

Sometimes, we want to change cursor when loading page in JavaScript

In this article, we’ll look at how to change cursor when loading page in JavaScript.

How to change cursor when loading page in JavaScript?

To change cursor when loading page in JavaScript, we can set the cursor CSS property of the body element.

For instance, we write:

document.body.style.cursor = 'wait';
setTimeout(() => {
  document.body.style.cursor = 'default';
}, 2000)

We set document.body.style.cursor to 'wait' to show the loading cursor.

Then we set document.body.style.cursor to 'default' to show the default arrow cursor after 2 seconds.

Conclusion

To change cursor when loading page in JavaScript, we can set the cursor CSS property of the body element.

Categories
JavaScript Answers

How to check for multiple string matches with JavaScript?

Sometimes, we want to check for multiple string matches with JavaScript.

In this article, we’ll look at how to check for multiple string matches with JavaScript.

How to check for multiple string matches with JavaScript?

To check for multiple string matches with JavaScript, we can use the JavaScript string’s match method with a regex.

For instance, we write:

const str = 'audio video'
if (str.match(/(video|audio)/)) {
  console.log('found')
} else {
  console.log('not found')
}

to call str.match with a regex.

Since 'audio' and 'video' are both in str, str.match(/(video|audio)/) returns true.

Conclusion

To check for multiple string matches with JavaScript, we can use the JavaScript string’s match method with a regex.

Categories
JavaScript Answers

How to wait until setInterval is done with JavaScript?

Sometimes, we want to wait until setInterval is done with JavaScript.

In this article, we’ll look at how to wait until setInterval is done with JavaScript.

How to wait until setInterval is done with JavaScript?

To wait until setInterval is done with JavaScript, we can call setInterval in a promise.

For instance, we write:

const rollDice = () => {
  return new Promise((resolve, reject) => {
    let numberOfRollsLeft = 5
    const intervalId = setInterval(() => {
      const diceValue = Math.floor(Math.random() * 6 + 1);
      numberOfRollsLeft--
      if (numberOfRollsLeft === 0) {
        clearInterval(intervalId);
        resolve(diceValue);
      }
    }, 50);
  });
}

const roll = async () => {
  const val = await rollDice()
  console.log(val)
}
roll()

We define rollDice which returns a promise.

We create the promise with the Promise constructor with a callback that calls setInterval with a callback to draw a random number.

If numberOfRollsLeft is 0, then we call clearInterval to stop the timer and call resolve with the diceValue to return that as the resolved value.

Next, we call rollDice in the roll function and use await to get the resolved promise value.

Conclusion

To wait until setInterval is done with JavaScript, we can call setInterval in a promise.

Categories
JavaScript Answers

How to filter strings in array based on content with JavaScript?

Sometimes, we want to filter strings in array based on content with JavaScript.

In this article, we’ll look at how to filter strings in array based on content with JavaScript.

How to filter strings in array based on content with JavaScript?

To filter strings in array based on content with JavaScript, we can use the JavaScript array’s filter instance method.

For instance, we write:

const myArray = ["bedroomone", "bedroomonetwo", "bathroom"];
const pattern = /bedroom/;
const filtered = myArray.filter((str) => {
  return pattern.test(str);
});
console.log(filtered)

We call myArray.filter with a callback that matches all the strings that has 'bedroom' in it by calling pattern.test with str.

Therefore, filtered is ['bedroomone', 'bedroomonetwo'].

Conclusion

To filter strings in array based on content with JavaScript, we can use the JavaScript array’s filter instance method.

Categories
JavaScript Answers

How to a remove portion of string in JavaScript?

Sometimes, we want to a remove portion of string in JavaScript.

In this article, we’ll look at how to a remove portion of string in JavaScript.

How to a remove portion of string in JavaScript?

To a remove portion of string in JavaScript, we can call the string replace method with the substring we want to remove and replace it with an empty string.

For instance, we write:

const string = '@usernameWhats on your mind?'
const newString = string.replace('Whats on your mind?', '');
console.log(newString)

We call string.replace to replace 'Whats on your mind?' with an empty string and return the new string.

Therefore, newString is '@username'.

Conclusion

To a remove portion of string in JavaScript, we can call the string replace method with the substring we want to remove and replace it with an empty string.