1

I want to loop all my table rows and do some validations, below is my code.

$("#table > tr").each( function( idx, el ) {

// my calculations goes here, it will take 500 ms for each record        

});

This is working fine when I do loop for 20-30 rows, if I do loop 100+ records then browser hangs.

I am unable to use setTimeout here in this logic, could some one help me out on this. Thanks in advance!!

11
  • It would be helpful to see the calculations, 500ms is a lot of time. Commented Nov 20, 2013 at 10:01
  • Show your validations code . Commented Nov 20, 2013 at 10:01
  • 500ms * 100 rows = 50 sec. If I'm not mistaking most modern browsers allow JS to run for 30 sec uninterrupted, that require user confirmation. So you'd better optimize the validations to consume less time. Commented Nov 20, 2013 at 10:02
  • what is your calculations about? Commented Nov 20, 2013 at 10:02
  • I just have no idea how you could possibly write code that takes more than a couple ms to run... How do you do that? Commented Nov 20, 2013 at 10:03

1 Answer 1

2

Of course you can add setTimeout:

$("#table > tr").each( function( idx, el ) {

    setTimeout( function() { validation(idx,el) }, 0 );

});

But I'm pretty sure you're doing something in a wrong way inside you validation function. 500 ms of synchronous code is just too much in web development, especially for 30sec tasks! I'm sure you can do it asynchronously or more optimised.

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

1 Comment

Thanks for reply, but is that seTimeout stops executing .each() untill we complete validation logic ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.