Categories
JavaScript Answers

How to create a sleep or delay in Node.js that is not blocking with JavaScript?

Sometimes, we want to create a sleep or delay in Node.js that is not blocking with JavaScript.

In this article, we’ll look at how to create a sleep or delay in Node.js that is not blocking with JavaScript.

How to create a sleep or delay in Node.js that is not blocking with JavaScript?

To create a sleep or delay in Node.js that is not blocking with JavaScript, we can use a promise.

For instance, we write

const sleep = (millis) => {
  return new Promise((resolve) => setTimeout(resolve, millis));
};

const test = async () => {
  await sleep(1000);
  console.log("one second has elapsed");
};

to define the sleep function that returns a promise that calls setTimeout which calls resolve after millis milliseconds.

Then we call sleep in test with 1000 to pause for 1000 milliseconds.

Conclusion

To create a sleep or delay in Node.js that is not blocking with JavaScript, we can use a promise.

Categories
React Answers

How to determine if the application is being viewed on mobile or desktop browser with React?

Sometimes, we want to determine if the application is being viewed on mobile or desktop browser with React.

In this article, we’ll look at how to determine if the application is being viewed on mobile or desktop browser with React.

How to determine if the application is being viewed on mobile or desktop browser with React?

To determine if the application is being viewed on mobile or desktop browser with React, we use the react-device-detect package.

To install it, we run

npm install react-device-detect --save

Then we use it by writing

import { isMobile } from "react-device-detect";

const MyComponent = () => {
  if (isMobile) {
    return <div> This content is available only on mobile</div>;
  }
  return <div> content... </div>;
};

to check is isMobile is true in MyComponent .

If it’s true, then the component is loaded on a mobile device.

Conclusion

To determine if the application is being viewed on mobile or desktop browser with React, we use the react-device-detect package.

Categories
JavaScript Answers

How to define a JavaScript regular expression that validates a password that has special characters?

Sometimes, we want to define a JavaScript regular expression that validates a password that has special characters.

In this article, we’ll look at how to define a JavaScript regular expression that validates a password that has special characters.

How to define a JavaScript regular expression that validates a password that has special characters?

To define a JavaScript regular expression that validates a password that has special characters, we can use the (?=.*[0-9]) and (?=.*[!@#$%^&*]) patterns.

For instance, we write

const regularExpression =
  /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{6,16}$/;

to define the regularExpression regex that has both groups.

(?=.*[0-9]) checks that the string has digits.

And (?=.*[!@#$%^&*]) checks that a string has at least 1 special character.

Conclusion

To define a JavaScript regular expression that validates a password that has special characters, we can use the (?=.*[0-9]) and (?=.*[!@#$%^&*]) patterns.

Categories
JavaScript Answers

How to get all checked checkboxes with JavaScript?

Sometimes, we want to get all checked checkboxes with JavaScript.

In this article, we’ll look at how to get all checked checkboxes with JavaScript.

How to get all checked checkboxes with JavaScript?

To get all checked checkboxes with JavaScript, we use querySelectorAll.

For instance, we write

const checkedBoxes = document.querySelectorAll(
  "input[name=mycheckboxes]:checked"
);

to call querySelectorAll with "input[name=mycheckboxes]:checked" to select all checkboxes with the name attribute set to mycheckboxes.

And we use :checked to select the checked ones with the attribute.

Conclusion

To get all checked checkboxes with JavaScript, we use querySelectorAll.

Categories
HTML

How to hide div by default and show it on click with Bootstrap?

Sometimes, we want to hide div by default and show it on click with Bootstrap.

In this article, we’ll look at how to hide div by default and show it on click with Bootstrap.

How to hide div by default and show it on click with Bootstrap?

To hide div by default and show it on click with Bootstrap, we add the data-toggle attribute.

For instance, we write

<a href="#Foo" class="btn btn-default" data-toggle="collapse">Toggle Foo</a>
<button href="#Bar" class="btn btn-default" data-toggle="collapse">
  Toggle Bar
</button>

<div id="Foo" class="collapse">This div is hidden by default</div>
<div id="Bar" class="collapse in">
  This div is shown by default and can toggle
</div>

to add an a element and a button that has the data-toggle attribyte set to collapse.

We also set the href attribute to the IDs of the divs.

And then we have 2 divs that has the collapse class which we can toggle with the a and button elements respectively.

The in class makes the div show by default.

Conclusion

To hide div by default and show it on click with Bootstrap, we add the data-toggle attribute.