Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

258
Views
Disable button until function is done

I am trying to disable the button until the code is successfully executed. Unfortunately, the button is activated too early, the function is still running. How can I prevent this?

$("#myButton").click(function() {
    $(this).prop("disabled", true)
    doSomethingFunction()
    $(this).prop("disabled", false)
});

Edit: Thank you all for your help. I have adjusted my code. Can you do it this way or are there better ways?

class TestClass
{
    static doSomethingFunction() {
        return new Promise(function (resolve, reject) {
            setTimeout(function () { console.log("function is done");  resolve(self);  }, 5000);
        })
    }
}

$("#myButton").click(function() {
    $(this).prop("disabled", true)
    TestClass.doSomethingFunction().then(r => $(this).prop("disabled", false))
});

The second solution does not work for me, because "completely done" is output before "function is done"

class TestClass
{
    static doSomethingFunction(callback) {
        setTimeout(function () { console.log("function is done");}, 2000);

        if(callback !== undefined){
            callback();
        }
    }
}

$("#myButton").click(function() {
    $(this).prop("disabled", true)

    TestClass.doSomethingFunction(function(){
        console.log("completely done")
    });
});

What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Consider the following.

var myData;

function doSomethingFunction(callback){
  $.get("someplace.php", function(data){
    myData = data;
    if(callback !== undefined){
      callback();
    }
  });
}

$("#myButton").click(function() {
  var $self = $(this);
  $self.prop("disabled", true);
  doSomethingFunction(function(){
    console.log(myData);
    $self.prop("disabled", false);
  });
});

This allows you to pass in code to run once the function is complete. In this example, maybe it's getting data from the server. This may take almost no time, or maybe the report takes 1.2 seconds to generate. Either way it will not be run until the AJAX is successful.

Update

Here is an example, based on the following: How do I use jQuery promise/deffered in a custom function?

$(function() {
  function doSomething() {
    var deferred = new $.Deferred();
    setTimeout(function() {
      deferred.resolve(10);
    }, 10 * 1000);
    return deferred;
  }

  $("button").click(function() {
    var $self = $(this);
    console.log("Disabled");
    $self.prop("disabled", true);
    doSomething().then(function() {
      $self.prop("disabled", false);
      console.log("Enabled");
    });
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>Start</button>

This will run for 10 seconds and then enable the button. Applying it to your example.

class TestClass {
  static doSomethingFunction() {
    var deferred = new $.Deferred();
    setTimeout(function() {
      deferred.resolve(10);
    }, 5 * 1000);
    return deferred;      
  }
}

$("#myButton").click(function() {
  var $self = $(this);
  $self.prop("disabled", true);
  TestClass.doSomethingFunction().then(r => $self.prop("disabled", false));
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!