Categories
JavaScript Answers

How to export arrow functions in JavaScript?

Sometimes, we want to export arrow functions in JavaScript.

In this article, we’ll look at how to export arrow functions in JavaScript.

How to export arrow functions in JavaScript?

To export arrow functions in JavaScript, we can use export directly with arrow functions.

For instance, we write

const hello = () => console.log("hello");
export default hello;

to export the hello function as a default export with export default.

We can also write

export const hello = () => console.log("hello");

to export hello as a named export.

Conclusion

To export arrow functions in JavaScript, we can use export directly with arrow functions.

Categories
JavaScript Answers

How to add delay between actions in JavaScript?

Sometimes, we want to add delay between actions in JavaScript.

In this article, we’ll look at how to add delay between actions in JavaScript.

How to add delay between actions in JavaScript?

To add delay between actions in JavaScript, we can create a function that returns a promise that delays the script by the time we want.

For instance, we write

const sleep = (ms) => {
  return new Promise((res) => setTimeout(res, ms));
};

const myAsyncFunc = async () => {
  console.log("Sleeping");
  await sleep(3000);
  console.log("Done");
};

myAsyncFunc();

to create the sleep function that returns a promise that calls setTimeout to call res in ms milliseconds.

Then we use await sleep in myAsyncFunc to delay the execution of the function by 3000 milliseconds.

Conclusion

To add delay between actions in JavaScript, we can create a function that returns a promise that delays the script by the time we want.

Categories
JavaScript Answers

How to prevent a webpage from navigating away using JavaScript?

Sometimes, we want to prevent a webpage from navigating away using JavaScript.

In this article, we’ll look at how to prevent a webpage from navigating away using JavaScript.

How to prevent a webpage from navigating away using JavaScript?

To prevent a webpage from navigating away using JavaScript, we can set the window.onbeforeunload property to a function that returns any value.

For instance, we write

window.onbeforeunload = () => {
  return "";
};

to set window.onbeforeunload to a function that returns an empty string to interrupt navigation with an alert box when we try to navigate to a different page.

Conclusion

To prevent a webpage from navigating away using JavaScript, we can set the window.onbeforeunload property to a function that returns any value.

Categories
JavaScript Answers

How to change href of an a tag on button click with JavaScript?

Sometimes, we want to change href of an a tag on button click with JavaScript

In this article, we’ll look at how to change href of an a tag on button click with JavaScript.

How to change href of an a tag on button click with JavaScript?

To change href of an a tag on button click with JavaScript, we can set the href property of the anchor element.

For instance, we write

<a href="#" id="abc">foo</a> <a href="#" id="myLink">bar</a>

to add 2 anchor elements.

Then we write

document.getElementById("myLink").onclick = (e) => {
  e.preventDefault();
  document.getElementById("abc").href = "example.com";
};

to get the anchor element with ID myLink with

document.getElementById("myLink")

Then we set its onclick property to a function that changes the href attribute of the anchor element with ID abc on click with

document.getElementById("abc").href = "example.com";

We call preventDefault to stop the default behavior of the link, which is to navigate to the URL set as the value of the href attribute.

Then we can run the rest of the code in the function.

Conclusion

To change href of an a tag on button click with JavaScript, we can set the href property of the anchor element.

Categories
JavaScript Answers

How to call a function periodically in JavaScript?

Sometimes, we want to call a function periodically in JavaScript.

In this article, we’ll look at how to call a function periodically in JavaScript.

How to call a function periodically in JavaScript?

To call a function periodically in JavaScript, we call the setInterval function.

For instance, we write

const intervalId = setInterval(() => {
  console.log("Interval reached every 5s");
}, 5000);

to call setInterval with a callback that runs every 5 seconds.

We specify the period with the 2nd argument in milliseconds.

setInterval returns the ID number of the timer.

And we can use clearInterval with the timer ID number to stop the timer.

Conclusion

To call a function periodically in JavaScript, we call the setInterval function.