-3

I have a Boolean function, and in some cases i need it to wait for 6 secs before returning.

I am trying to do something like:

function myFunc() {
   if (pending) {
       setTimeout(return true, 6000);
   } else {
       return false;
   }
}

The reason I use setTimeout, is that i have some other code running in the same time, and I want to be able to cancel the setTimeout.

Can anyone please help?

Thank you!!

yogev
  • 149
  • 8
  • 1
    no way!!! you need to have a look at how to make use of callbacks... also see http://stackoverflow.com/questions/23667086/why-is-my-variable-undefined-after-i-modify-it-inside-of-a-function-canonical – Arun P Johny Aug 06 '14 at 10:03
  • thanks, but I am well aware of the use of callbacks, and I read what was was said in those like you posted. yet it does not answer my question... I need the outer function to return after 6 seconds. – yogev Aug 06 '14 at 10:08
  • In this case you need promises. Something like this https://www.promisejs.org/, but you can choose other implementation as well. – Anton L Aug 06 '14 at 10:09
  • Can you explain more widely what are you trying to achieve with delaying return from function? – Justinas Aug 06 '14 at 10:57
  • Yes: What i called "myFunc" is actually a jQuery validator costume function. So you see, that function is being called upon submitting a form. In some cases, i need the form to wait for a response from the server before it's being submitted. So i must NOT return ANY value until i get a response, or if 6 seconds has passed - which ever happens sooner – yogev Aug 06 '14 at 10:59
  • Since you are using jQuery AJAX request, use `success` function to do callback, there is no other way. What if you wait 5 seconds and response comes after 6 seconds? – Justinas Aug 06 '14 at 11:01
  • if i have waited for 6 seconds and still no response - I want the validator to return 'true' and let the form be submitted. This is why "success" callback will not work for me. – yogev Aug 06 '14 at 11:03

1 Answers1

1

The best way is to recall the function after some delay

function myFunc() {
   if (pending) {
       setTimeout(myFunc(), 6000);
   } else {
       //run the procedure that must be called if not pending
   }
}
Ahmed Daif
  • 299
  • 3
  • 11
  • WOW! cool idea! I will try that. Thanks – yogev Aug 06 '14 at 10:15
  • Mmm.. not good. The first iteration of the function still returns immediately. – yogev Aug 06 '14 at 10:45
  • yes it should return immediately. what I mean is to write the code that you want to run if it returned false in the commented area – Ahmed Daif Aug 06 '14 at 10:49
  • it's kind of a problem, since what i called "myFunc" is actually a jQuery validator costume function. So you see, that function is being called upon submitting the form. In some cases, i need the form to wait for a response from the server before it's being submitted. So i must NOT return ANY value until i get a response, or if 6 seconds has passed - which ever happens sooner – yogev Aug 06 '14 at 10:58