Лучший способ применить к максимальному таймауту для функции обратного вызова в Javasciprt - PullRequest
0 голосов
/ 17 июня 2020

Я хочу применить максимальный тайм-аут к одной функции, чтобы задача не была выполнена в заданное время, она просто уведомляет вызывающую функцию, она просто переходит к следующему шагу. 1005 *. (ES5) Вот Самел, чтобы объяснить мой запрос.



function dowatch() {
  var cnt = 0;
    //function to keep checking (max timeout )if in given time,result is achieved or not and move to next steps
//should setTimeout of max time can be used instead of regular check?
    var hnd = setInterval(function() {
        cnt += 1;
        //max try is 5 times on 250ms regular interval to check window.cdoneis defined or not
        if (cnt >= 5 || (window.cdone)) {
            clearInterval(hnd);
            // call function anyway
            notifyTotohers();
        }
    }, 250);
}

function getDataFrmExtSource(cb) {
    //here call ajax call  & read response or inject iframe and read data from it
    //...

    //once done ,callback or raise window.customEvent()
    typeof cb == 'function' && cb();
}

//call back function
function taskdone() {
    window.cdone = true;//this will cancel the setinerval
    notifyTotohers();
}

function notifyTotohers() {
    //method continue other task called only once
if(!window.notified){window.notified=true;
//calltoher fucntion
}

}

//main function
try {
    //get data from ifram or ajax -external source
    getDataFrmExtSource(taskdone);

} catch (ex) {} finally {
//start timer to keep checking on regular interval till max time reached.
    dowatch();
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...