0

I'm working on a script which handles a local/distant synchronisation of files (check/delete/download).

This is working quite fine, but I struggle to know when the tasks are finished. Therefore, I have an incrementation of the tasks to perform.

I would like to do something like this, but I don't know how to code it properly:

function wait(){
    setTimeout(function(){
        if (fileCount<totalCount){
            wait();
        }else{
            clearTimeout();
            taskAreFinished();
        }
    },100);
}

I know this code is ugly; it's just to share the idea...

Yako
  • 3,405
  • 9
  • 41
  • 71
  • 1
    Learn about promises: http://learn.jquery.com/code-organization/deferreds/. The call to `clearTimeout` is certainly unnecessary. You can remove it. – Felix Kling Mar 23 '14 at 21:42
  • 1
    You need to assign `var thisTime = setTimeout(function(){/*do stuff*/}, 100)` to a `var` and pass it to `clearTimeout(thisTime)`. – StackSlave Mar 23 '14 at 21:45
  • @PHPglue: That's not necessary. At the moment `clearTimeout(thisTime)` is called, the timeout already executed and cannot be cleared. What is important is to not call `wait` again, which the OP already does. – Felix Kling Mar 23 '14 at 22:40
  • Thanks for your comments. So far, it seems to be working without the clearTimeout(). @Felix, I didn't know about these learning pages of the jQuery doc. The Defereds looked quite complicated to me, but these explanations seem detailed; so I'm gonna try to "translate" my code in this jQuery way. – Yako Mar 24 '14 at 09:02

0 Answers0