Categories
JavaScript Answers

How to force a script reload and re-execute with JavaScript?

Sometimes, we want to force a script reload and re-execute with JavaScript.

In this article, we’ll look at how to force a script reload and re-execute with JavaScript.

How to force a script reload and re-execute with JavaScript?

To force a script reload and re-execute with JavaScript, we can make a copy of the original script, remove it, and then append it again.

For instance, we write

const scriptTag = document.createElement("script");
scriptTag.innerText = "document.body.innerHTML += 'Here again ---<br>';";
const head = document.getElementsByTagName("head")[0];
head.appendChild(scriptTag);

setInterval(() => {
  head.removeChild(scriptTag);
  const newScriptTag = document.createElement("script");
  newScriptTag.innerText = scriptTag.innerText;
  head.appendChild(newScriptTag);
  scriptTag = newScriptTag;
}, 1000);

We create the scriptTag with createElement.

And then we set its innerText property to add the script we want to run inside.

Next, we get the head element with getElementsByTagName.

Then we call head.appendChild with scriptTag to append it to the head.

In the setInterval callback, we remove the original scriptTag with head.removeChild.

Then we we recreate the script tag as newScriptTag and append it again to the head with head.appendChild.

Conclusion

To force a script reload and re-execute with JavaScript, we can make a copy of the original script, remove it, and then append it again.

Categories
JavaScript Answers

How to find the time left in a setTimeout() timer with JavaScript?

Sometimes, we want to find the time left in a setTimeout() timer with JavaScript.

In this article, we’ll look at how to find the time left in a setTimeout() timer with JavaScript.

How to find the time left in a setTimeout() timer with JavaScript?

To find the time left in a setTimeout() timer with JavaScript, we can use the timer’s _idleStart and _idleTimeout properties.

For instance, we write

const timeout = setTimeout(() => {}, 3600 * 1000);

const getTimeLeft = (timeout) => {
  return Math.ceil(
    (timeout._idleStart + timeout._idleTimeout - Date.now()) / 1000
  );
};

setInterval(() => {
  console.log("Time left: ", getTimeLeft(timeout), "s");
}, 2000);

to create the timeout timer object with setTimeout.

Then we calculate the time left before running the setTimeout callback with the getTimeLeft function.

In it, we use timeout._idleStart + timeout._idleTimeout - Date.now() to compute the time left before the setTimeout callback is run in milliseconds.

Then we divide the ceiling of the number and divide it by 1000 to get the number of seconds.

Next, we call setInterval with a callback that calls getTimeLeft with timeout in the callback to get the number of seconds left before the setTimeout callback is called every 2 seconds.

Conclusion

To find the time left in a setTimeout() timer with JavaScript, we can use the timer’s _idleStart and _idleTimeout properties.

Categories
JavaScript Answers

How to evaluate a string as a mathematical expression in JavaScript?

Sometimes, we want to evaluate a string as a mathematical expression in JavaScript.

In this article, we’ll look at how to evaluate a string as a mathematical expression in JavaScript.

How to evaluate a string as a mathematical expression in JavaScript?

To evaluate a string as a mathematical expression in JavaScript, we can use the mathjs library.

To install it, we run

npm install mathjs

Then we can use it by writing

import { evaluate } from "mathjs";

const n = evaluate("1.2 * (2 + 4.5)");

to call evaluate with the math expression we want to evaluate.

The return value is the value of the expression after evaluating as a number.

Conclusion

To evaluate a string as a mathematical expression in JavaScript, we can use the mathjs library.

Categories
JavaScript Answers

How to use getElementsByClassName() with two classes with JavaScript?

Sometimes, we want to use getElementsByClassName() with two classes with JavaScript.

In this article, we’ll look at how to use getElementsByClassName() with two classes with JavaScript.

How to use getElementsByClassName() with two classes with JavaScript?

To use getElementsByClassName() with two classes with JavaScript, we can use the querySelectorAll method.

For instance, we write

const els = document.querySelectorAll(".a,.b");

to call document.querySelectorAll to select all elements on the page with classes a and b.

We separate each class with commas in the selector.

Conclusion

To use getElementsByClassName() with two classes with JavaScript, we can use the querySelectorAll method.

Categories
JavaScript Answers

How to remove ‘&quot’ from JSON in JavaScript?

Sometimes, we want to remove ‘&quot’ from JSON in JavaScript.

In this article, we’ll look at how to remove ‘&quot’ from JSON in JavaScript.

How to remove ‘&quot’ from JSON in JavaScript?

To remove ‘&quot’ from JSON in JavaScript, we call the string replace method.

For instance, we write

const obj = JSON.parse(data.replace(/&quot;/g, '"'));

to call data.replace with /&quot;/g and '"' to replace all instances of '&quot' with double quotes on the data string.

Then we call JSON.parse to parse the string returned by replace into the obj object.

Conclusion

To remove ‘&quot’ from JSON in JavaScript, we call the string replace method.