Javascript JS重试函数多次,以查看它是否返回true
我正在寻找一种更好的方法,在函数返回true或false时重试Javascript JS重试函数多次,以查看它是否返回true,javascript,Javascript,我正在寻找一种更好的方法,在函数返回true或false时重试 function foo() { // var tabList = window.content.document.getElementById('compTabs') // this might be null if page is not loaded and further code wont work if (!tabList) { // stop here if tab list i
function foo() { //
var tabList = window.content.document.getElementById('compTabs') // this might be null if page is not loaded and further code wont work
if (!tabList) { // stop here if tab list is null
return false;
}
// continue and finish function
}
// this is a loop that will go trough an array and this check needs to happen for each element of the array
for (var i; i < loopLenght; i++) {
// This is the actual code nothing else happens here.
if ( !foo() ) {
// try again
if ( !foo() ) {
// try one more time
if ( !foo() ) {
console.log('Failed')
}
}
}
// a lot more code coming here that should only run one per iteration
}
函数foo(){//
var tabList=window.content.document.getElementById('compTabs')//如果页面未加载,并且进一步的代码无法工作,则此值可能为空
如果(!tabList){//如果制表符列表为空,则停止此处
返回false;
}
//继续并完成函数
}
//这是一个通过数组的循环,需要对数组的每个元素进行检查
for(变量i;i
我只是在寻找一种更好、更简洁的方法来编写上面的代码。您是在浏览器中吗?(与节点等相对)
是否必须重试N次
while (!condition) { }
但那会挡住并挂断你的线
如果你想投票
function whileConditionNotMet()
{
if (!condition) {
setTimeout(whileConditionNotSet, 1000);
return false;
}
// Condition met
// ...
return true;
}
您可以通过增加一个静态变量来限制它的检查次数:
function whileConditionNotMet()
{
if ( typeof whileConditionNotMet.counter == 'undefined' ) {
whileConditionNotMet.counter = 0;
}
if (whileConditionNotMet.counter++ > 10) {
// Timeout
// ...
return false;
}
if (!condition) {
setTimeout(whileConditionNotSet, 1000);
return false;
}
// Condition met
// ...
return true;
}
……或者
var counter = 0;
while (!condition && counter++ < 10) { }
var计数器=0;
而(!条件和计数器+++<10){}
在这里,重试--
对每个循环迭代进行倒计时,执行foo()
并将结果保存到success
中
如果重试
点击0
或success
变为true
,循环停止。不需要循环体
警告:如果foo()
是一个异步函数,这将不起作用。请参阅我写的内容:
// -------------- RETRIES CLASS 151118 -------------------
// executeFunction - Returns true if success, false otherwise
// limitRetries - max number of retries
// sleepInBetweenMilli - the delay in millis between one retry to another
function Retrier(executeFunction, limitRetries, sleepInBetweenMilli, successCallback, failCallback) {
this.executeFunction = executeFunction;
this.limitRetries = limitRetries;
this.sleepInBetweenMilli = sleepInBetweenMilli;
this.successCallback = successCallback;
this.failCallback = failCallback;
this.thisRetry = 1;
var RetrierObjectThis = this;
function doCycle() {
var result = RetrierObjectThis.executeFunction();
if (result == true) {
RetrierObjectThis.successCallback();
} else {
if (RetrierObjectThis.thisRetry >= RetrierObjectThis.limitRetries) {
RetrierObjectThis.failCallback();
} else {
RetrierObjectThis.thisRetry++;
setTimeout(doCycle, RetrierObjectThis.sleepInBetweenMilli);
}
}
}
doCycle();
}
您应该添加完整的代码,而不仅仅是伪代码。请参阅和。while(true)如何?使用循环会更整洁,并且便于以后更改重试次数。或者,如果您不想在两次重试之间执行任何特定操作,您可以使用单个if语句(利用短路计算):
if(!foo()&&&!foo()&&&&!foo()){/*failed*/}否则{/*succeed*/}
@RayonDabre如果他这样做的话,他最终会得到无限循环。可能var i=0;而(foo()| | i@BozidarSikanjic,是的…更有意义…没有节点,一个javascript+jquery脚本在FireFox中运行。这取决于您需要发生什么-(即,如果轮询延迟无关紧要,vs您需要尽可能快地响应UI)-您可能会考虑查看一些后台任务在轮询中咀嚼(见下文),否则我会作弊并使用SETIMEOTUTE()来处理周期性的“唤醒和检查状态”。-因此,您不会过多地阻止用户交互。这很接近,但如果重试5次后仍然失败,我需要阻止进一步的代码发生,这意味着在成功时添加另一个if条件,试图尽可能缩短代码。并且…?您有重试
的值和成功
之后的值在循环中,我很确定你能想出一些办法。(我不确定为什么“如果
条件”添加另一个是不可取的。)
// -------------- RETRIES CLASS 151118 -------------------
// executeFunction - Returns true if success, false otherwise
// limitRetries - max number of retries
// sleepInBetweenMilli - the delay in millis between one retry to another
function Retrier(executeFunction, limitRetries, sleepInBetweenMilli, successCallback, failCallback) {
this.executeFunction = executeFunction;
this.limitRetries = limitRetries;
this.sleepInBetweenMilli = sleepInBetweenMilli;
this.successCallback = successCallback;
this.failCallback = failCallback;
this.thisRetry = 1;
var RetrierObjectThis = this;
function doCycle() {
var result = RetrierObjectThis.executeFunction();
if (result == true) {
RetrierObjectThis.successCallback();
} else {
if (RetrierObjectThis.thisRetry >= RetrierObjectThis.limitRetries) {
RetrierObjectThis.failCallback();
} else {
RetrierObjectThis.thisRetry++;
setTimeout(doCycle, RetrierObjectThis.sleepInBetweenMilli);
}
}
}
doCycle();
}