Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript How can I add a timeout in between functions?

Johna

Frontend Developer
Staff Team
Guardian
HTML:
<button id="button">Click me!</button>
JavaScript:
$("#button").click(function() {
  console.log("clicked");
})
The button can be clicked any number of times, but I want the JavaScript function to only run at most once per second. So if the button was clicked twice in a second, the function will only run once.


I can't use setTimeout because the function will start a second late, which I don't want to happen. It'll also still run twice if the button was clicked twice within a second.

Is there a way to do this?
 
Solution
HTML:
<button id="button">Click me!</button>
JavaScript:
$("#button").click(function() {
  console.log("clicked");
})
The button can be clicked any number of times, but I want the JavaScript function to only run at most once per second. So if the button was clicked twice in a second, the function will only run once.


I can't use setTimeout because the function will start a second late, which I don't want to happen. It'll also still run twice if the button was clicked twice within a second.

Is there a way to do this?
What you could do, is after the button is clicked, run what you need to do, disable it. Unfortunately, you're still going to have to run a setTimeout, however, you can set the timeout to say 3 seconds, and at...
HTML:
<button id="button">Click me!</button>
JavaScript:
$("#button").click(function() {
  console.log("clicked");
})
The button can be clicked any number of times, but I want the JavaScript function to only run at most once per second. So if the button was clicked twice in a second, the function will only run once.


I can't use setTimeout because the function will start a second late, which I don't want to happen. It'll also still run twice if the button was clicked twice within a second.

Is there a way to do this?
What you could do, is after the button is clicked, run what you need to do, disable it. Unfortunately, you're still going to have to run a setTimeout, however, you can set the timeout to say 3 seconds, and at the end, have it reenable the button
 
Solution
Thanks, for this I can use a setTimeout, I meant I can't use a setTimeout like this:
JavaScript:
$("#button").click(function() {
  setTimeout(function() {
    console.log("clicked");
  }, 1000)
})
 
So I did it like this:
JavaScript:
$("#button").click(function() {
  $("#button").prop("disabled", true);
  console.log("clicked");
  setTimeout(function() {
    $("#button").prop("disabled", false);
  }, 1000)
})
 
Back
Top Bottom