4

Is there any way to run my function within 5 seconds in Javascript?

For example if I have Function A and Function B. I want to run Function A for 5 seconds, just after it, it will run Function B.

  • IF Function A only takes 1 second to finish the process, then it only need to wait another 4 seconds.
  • But if Function A takes 3 seconds to finish, then it would need another 2 seconds to wait, before processing Function B
  • IF Function A takes more than 5 seconds, then B needs to wait until A finish its job, until it can start its job. (edited)

Right now, im using Sleep Function that I found not long time ago

function wait(ms){ var start = new Date().getTime(); var end = start; while(end < start + ms) { end = new Date().getTime(); } } wait(5000); 

But with this code, it will need to wait for 5 seconds no matter how much time Function A needs to finish its process.

EDIT

Function A does an AJAX POST call. which makes it takes different time to finish its job.

This question is different from What is the JavaScript version of sleep()? because :

  1. this is going to be used in IE which doesn't support promise (as I read)
  2. If I use settimeout, and A takes longer than 5 seconds, then B will fire it function first without waiting A to finish.
6
  • Don't you mean function a takes 7 seconds? Commented Sep 8, 2016 at 7:17
  • Is FunctionA performing asynchronous work? Or does it take 1 second for other reasons? Commented Sep 8, 2016 at 7:17
  • setTimeout, setInterval, $evalAsync, $applyAsync, web workers can do the job Commented Sep 8, 2016 at 7:19
  • 1
    A combination of setTimeout() and a promise() should do. Commented Sep 8, 2016 at 7:19
  • @casraf @Juhana Function A is Ajax POST call, which makes it finish the job in different time, depends on the process. Commented Sep 8, 2016 at 7:22

1 Answer 1

3

I see these easiest approaches to this:

  1. If Function A is asynchronous (e.g. sending an AJAX request and waiting for reply), simply use setTimeout, as the request will be done in the background and not freeze the main process. So funcA() will be immediately done running on the UI thread, then the next expression will immediately take place, (which waits for 5 seconds), then the callback from the AJAX response will be called whenever it's done fetching the response.

    funcA(); setTimeout(funcB, 5000); 
  2. If Function A takes up to 5 seconds for other reasons, and you need the wait to be more dynamic, you could probably time it and subtract:

    var startTime = new Date().getTime(), endTime; funcA(); endTime = new Date().getTime(); setTimeout(funcB, 5000 - (endTime - startTime)); 
  3. If Function A might take longer than 5 seconds, and Function B needs to wait for it, I suggest something similar to this:

    function funcA() { var startTime = new Date().getTime(), endTime; // ... code ... $.post(..., { ..., // when done/resolved the AJAX request -> success: function(response) { endTime = new Date().getTime(); setTimeout(funcB, 5000 - (startTime - endTime)); } }); } 
Sign up to request clarification or add additional context in comments.

9 Comments

What if funcA takes 5001(or more) milliseconds to finish? waitTime = 5000 - (endTime - startTime); if(waitTime < 0) { waitTime = 0; }
@aifrim Not necessary, because a negative timeout is automatically converted to the minimum.
what would happen if I use the first approach, but then FunctionA takes more than 5 seconds to finish. Would Func B do its job first without waiting the reply from FunctionA?
Yes, it will. If you really wanna get that deep into it, you can use some sort of condition that reflects if the task has been done, and try again every few milliseconds until that response is correct.
@alfrim no, if it performs async stuff, it literally will not wait for it to finish before proceeding with the task.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.