Run a function only for a set amount of time with Javascript (no jQuery)

Kunj Parikh

I am a newbie.

How do I call a function for a set amount of time, say 3000 milliseconds? I have tried setInterval and setTimeout but they don't work.

if (budgeting === '' || budgeting.indexOf('e') > -1 || budgetValue < 0) {
  let errorHandler = document.createElement('div');
  let textErrorHandler = document.createTextNode('Oops! The balance you entered is invalid. Try again!')
  errorHandler.style.width = '500px';
  errorHandler.style.height = '20px';
  errorHandler.style.background = 'coral';
  errorHandler.style.float = 'left';
  errorHandler.appendChild(textErrorHandler);
  document.body.append(errorHandler);
}

The above is my code currently. What I'm trying to do is display an error message box for 3 seconds(3000 milliseconds), but I want it to only display each time the error occurs, i.e, only once.

RobG

When displaying the error, set a timeout to hide it again after an interval. If there's a timeout running when setting the timeout, clear it and set a new one.

The following is a basic implementation. The "Show error" button can be clicked as often as required, it will only show one message, only set one timeout at a time and only linger for the specified interval after the last click:

let showError = (() => {
  let timeoutID;
  return () => {
    let el = document.getElementById('err0');
    el.style.visibility = 'visible';
    // Clear timeout if one might be running
    if (timeoutID) clearTimeout(timeoutID);
    // Set timer for 1,000 ms (1 second), clear timeoutID when finished
    timeoutID = setTimeout(()=>{
      el.style.visibility = 'hidden'; 
      timeoutID = null;
    }, 1000);
  };
})();
#err0 {
  visibility: hidden;
  color: red;
}
<button onclick="showError()">Show error</button>
<span id="err0">Error</span>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Make a function run when a certain amount of time has passed with JavaScript?

Have a function run for a specific amount of time in C

How can i trigger this jquery script to automatically run after a set amount of time (5 seconds maybe)?

Change a value over a set amount of time with jQuery?

Run javascript function when enter is pressed - JavaScript only no Jquery

Running a function a set amount of times in Jquery

Is there a way to execute a JavaScript/JQuery function on my html page for a specified amount of time then stop the execution?

How to set a interval with Puppeteer to run a certain amount of time?

How can I run a method repeatedly for a set amount of time?

Only run JavaScript function once

How to disable a jquery function from executing for a certain amount of time

Set time to execute Javascript function?

Run the function only once at the same time

jQuery fire a function after set amount of pixels is scrolled

Formatting Time, Amount In Javascript

How to kill Javascript function after a certain amount of time

What is the run time of the set difference function in Python?

jquery.datetimepicker set time only

jQuery function only being run once

jQuery - Run function only if list is empty

Jquery: run function only every x seconds

Why does my code only work with small amount of data?(run-time error 1004-VBA)

Run JavaScript function at regular time interval

only run javascript function after certain date

Run function only once - functional programming JavaScript

run JavaScript function only if Boolean is true

Run JavaScript function only if user logged in - Django

confirm only one time function with jquery

jquery .load() function only working the first time