David
David

Reputation: 1861

Javascript - I'd like to show the checkbox check before a long running function runs

<input type="checkbox" onclick="myFunc()" />

myFunc is somewhat long running, about a second, and the browser shows the check for the checkbox AFTER the function completes. This causes lots of issues with users.

I'd like the check to show up immediately and then the onlick function to run. Can that be done?

I know I should get the function to run faster, or redesign the user interface, but that's another issue for another time.

thanks

Upvotes: 3

Views: 92

Answers (2)

Manuel van Rijn
Manuel van Rijn

Reputation: 10305

first of all you should not use onclick="myFunc()"

here's my solution:

$(function() {
    $("input[tpe='checkbox']").click(function(e) {
        e.preventDefault();
        $(this).attr("checked", "checked");
        // your code here ...
    });
});

Upvotes: 0

Prusse
Prusse

Reputation: 4315

Try:

<input type="checkbox" onclick="setTimeout(myFunc, 0);" />

Upvotes: 2

Related Questions