You must wait for several conditions to be met.

  1. End of shared timer (_TimeOver);

  2. The end of the current timer (_CurrentTimer);

  3. The end of the audio playback (if at the time of the termination of the above events 1 and 2 it is played) (_AudioInstructionEnd).

C event 1 can be caught through the deferred object. But with events 2 and 3 problems arise - each time the button is pressed, their status is updated. The logic for these two events, as I understand it, is unlikely to be different.

It should turn out that type:

$.when(_TimeOver, _CurrentTimer, _AudioInstructionEnd).then(Finish); //когда выполнены все условия запустить функцию финиш 
  • one
    for the sake of completeness, there is not enough description of what is in the variables. - Ivan Pshenitsyn
  • These are conditional deferred objects that pop up upon completion of events (_CurrentTimer - upon execution of setTimeout, _AudioInstructionEnd - upon completion of audio playback) - Denis Bozhkov

1 answer 1

 $(function(){ // В Deferred-объектах делаем что-то, при завершении, вызываем resolve для изменения состояния // В примерах это просто таймеры let _TimeOver = $.Deferred(obj => { setTimeout(() => obj.resolve(), 1500); }); let _CurrentTimer = $.Deferred(obj => { setTimeout(() => obj.resolve(), 1700); }); let _AudioInstructionEnd = $.Deferred(obj => { setTimeout(() => obj.resolve(), 2000); }); // Собираем все Deffered-объекты в один и когда он успешно разрешится, выполнится done, иначе - fail $.when(_TimeOver, _CurrentTimer, _AudioInstructionEnd).done(r => console.info('Ho-ho-ho!')).fail(e => console.info(':(')); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

  • @ Alexey Shimansky, congratulations, your encryption is so good that I don’t know what she is doing here, under this answer :) - user207618