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.

Marcel Angir
  • 169
  • 1
  • 1
  • 11

2 Answers2

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.

Matt
  • 5,315
  • 1
  • 30
  • 57
  • 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. – Marcel Angir Jun 21 '17 at 05:47
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.

Alexander
  • 4,420
  • 7
  • 27
  • 42
  • Why? And how will it make the function execute faster? – Tushar Jun 21 '17 at 04:56
  • @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. – Alexander Jun 21 '17 at 04:59
  • @Tushar, I find [the topic](https://stackoverflow.com/a/779785/7914637) that answers to your question. – Alexander Jun 21 '17 at 05:12
  • 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. – Marcel Angir Jun 21 '17 at 05:16
  • @MarcelAngir, also you can try to use `0` timeout value. But in my case I was needed to use positive value of timout. – Alexander Jun 21 '17 at 05:18
  • thank you very much @Alexander – Marcel Angir Jun 21 '17 at 05:45