0

I have an event :

$(document).on('change', '.many-checkbox', function() {
  doSomething()
});

doSomething() is kinda heavy, so checkbox with class many-checkbox will have a delay before they actually being checked/uncheck.

I want to remove those delays.

So, how do we check/uncheck it before doSomething() is done ?.

thanks.

5
  • Can you reproduce this in StackSnippet/JSFiddle? Looks like the problem could be somewhere else. Or in doSomething. Commented Jun 21, 2017 at 4:48
  • 1
    BTW, $(this).prop('checked', $(this).prop('checked')); does NOTHING but adds delay. Commented Jun 21, 2017 at 4:49
  • I'm sure the problem is in doSomething, that's why i want to bypass it. So, make the checkbox checked/uncheck manually even before doSomething is done. Commented Jun 21, 2017 at 4:50
  • Yeah, I know my solution is kinda ridiculous. I've removed it. Commented Jun 21, 2017 at 4:51
  • Can doSomething be made asynchronous? Commented Jun 21, 2017 at 4:52

2 Answers 2

0

You could replacr doSomething() function to setTimout method. For example:

$(document).on('change', '.many-checkbox', function() {
  setTimeout(function(){ 
    // do something
  }, 100);
});

In result the JavaScript execution will be paused to let the rendering threads catch up.

Sign up to request clarification or add additional context in comments.

6 Comments

Why? And how will it make the function execute faster?
@Tushar, this will not be faster. But it will be executed after another actions. I'm sorry, but I do not remember a source of this solution.
@Tushar, I find the topic that answers to your question.
Ah, i see. It means wait for 0.01 second before do doSomething(), resulting the checkbox being checked first between 0.01 second. Its not making it faster, but it bypass the proccess by 0.01 second.
@MarcelAngir, also you can try to use 0 timeout value. But in my case I was needed to use positive value of timout.
|
0

I suspect there's all sorts of wrong going on here but you could start by not listening to any change within the entire body and filtering down to the .many-checkbox class.

Listen directly to changes to the checkboxes:

$('.many-checkbox').on('change', function() {
  doSomething();
});

If you show us what doSomething is doing, we could maybe make it asynchronous.

1 Comment

I'm sorry, its way to complicated to be made async. As you say, there's a lot of mistake on it, my bad code ~ But thank you very much for your answer, appreciate it so much.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.