I want to apply max timeout to one fucntion so task is not done in given time, it just notify the calling fuction it just move to next step.
what is best way to do it in pure Javascript.(ES5) here is Samelto explain my query.
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();
}