Categories
JavaScript Answers

How to add simple throttling in JavaScript?

Sometimes, we want to add simple throttling in JavaScript.

In this article, we’ll look at how to add simple throttling in JavaScript.

How to add simple throttling in JavaScript?

To add simple throttling in JavaScript, we can create our own function.

For instance, we write

const throttle = (func, timeFrame) => {
  let lastTime = 0;
  return () => {
    const now = Date.now();
    if (now - lastTime >= timeFrame) {
      func();
      lastTime = now;
    }
  };
};

to define the throttle function that takes the func function and the timeFrame in milliseconds that func is allowed to run once.

In it, we return a function that checks now - lastTime is bigger than or equal to timeFrame.

If it is, then we call func since it hasn’t been called within the timeFrame.

And then we set lastTime to now.

now is the current datetime’s timestamp in milliseconds.

Conclusion

To add simple throttling in JavaScript, we can create our own function.

Categories
JavaScript Answers

How to subtract days, months, years from a date in JavaScript?

Sometimes, we want to subtract days, months, years from a date in JavaScript.

In this article, we’ll look at how to subtract days, months, years from a date in JavaScript.

How to subtract days, months, years from a date in JavaScript?

To subtract days, months, years from a date in JavaScript, we can call setDate, setMonth, and setFullYear.

For instance, we write

const createDate = (days, months, years) => {
  const date = new Date();
  date.setDate(date.getDate() + days);
  date.setMonth(date.getMonth() + months);
  date.setFullYear(date.getFullYear() + years);
  return date;
};

to define the createDate function.

In it, we create a Date object.

We call setDate with the date value with days added to it to add the number of days to the current date, which we get from getDate.

We call setMonth with the date value with days added to it to add the number of months to the current date, which we get from getMonth.

And we call setFullYear with the date value with days added to it to add the number of years to the current date, which we get from getFullYear.

Finally, we return the date with the values added.

We call createDate with negative values for each argument to subtract them.

Conclusion

To subtract days, months, years from a date in JavaScript, we can call setDate, setMonth, and setFullYear.

Categories
JavaScript Answers

How to set image source with base64 with JavaScript?

Sometimes, we want to set image source with base64 with JavaScript.

In this article, we’ll look at how to set image source with base64 with JavaScript.

How to set image source with base64 with JavaScript?

To set image source with base64 with JavaScript, we can set the src property to a base64 URL string.

For instance, we write

document.getElementById("img").src =
  "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";

to select the img element with getElementById.

Then we set its src property to the base64 image URL string to set the src attribute of the img element to that value.

Conclusion

To set image source with base64 with JavaScript, we can set the src property to a base64 URL string.

Categories
JavaScript Answers

How to fix Node.js Express.js app only working on port 3000 with JavaScript?

Sometimes, we want to fix Node.js Express.js app only working on port 3000 with JavaScript.

In this article, we’ll look at how to fix Node.js Express.js app only working on port 3000 with JavaScript.

How to fix Node.js Express.js app only working on port 3000 with JavaScript?

To fix Node.js Express.js app only working on port 3000 with JavaScript, we can set the port with the process.env.PORT property.

For instance, we write

app.set("port", process.env.PORT || 3000);

to set the port of the Express app with app.set.

We try to get the port number from the PORT environment variable, which is process.env.PORT.

Otherwise, we use port 3000 as the fallback.

Then we start our Node Express app by running

PORT=8080 node app.js

to set the PORT environment variable to 8080 and run app.js which has the app.set code.

Conclusion

To fix Node.js Express.js app only working on port 3000 with JavaScript, we can set the port with the process.env.PORT property.

Categories
JavaScript Answers

How to force JavaScript to do math instead of putting two strings together?

Sometimes, we want to force JavaScript to do math instead of putting two strings together.

In this article, we’ll look at how to force JavaScript to do math instead of putting two strings together.

How to force JavaScript to do math instead of putting two strings together?

To force JavaScript to do math instead of putting two strings together, we should make sure we’re using arithmetic operators with numbers.

For instance, we write

const x = +"11.5" + +"3.5";

to convert '11.5' and '3.5' to numbers with the unary + operator.

Then we add the returned numbers together and assign the sum to x.

Conclusion

To force JavaScript to do math instead of putting two strings together, we should make sure we’re using arithmetic operators with numbers.