gpt4 book ai didi

javascript - 用Javascript检查2个互斥条件的最佳方法?

转载 作者:行者123 更新时间:2023-11-28 21:03:39 26 4
gpt4 key购买 nike

您好,我有以下代码,用于检查 Rails 模型状态

var intervalCall = setInterval(function(){
$.post("getstatus", {id:id});
var finished = "<%= @sentence.finished%>";
//THIS IS CONDITION ONE, IT IS LIKELY TO HAPPEN LATER AND I WANT TO STOP THE SETINTERVAL
if ("<%= @sentence.result %>"){
clearInterval(intervalCall);
state_2();
}
//THIS IS CONDITION TWO, IT IS LIKELY TO HAPPEN EARLIER AND I WANT
// TO KEEP THE SETINTERVAL RUNNING AFTER IT"S MET
else if (String(finished)== "true"){
state_1();
}
},3000);


intervalCall;

组织这种流程的最佳方式是什么?

提前致谢!

最佳答案

// setInterval/ setTimeout return the timer id
var intervalCall;
function updateStatus (){
// post need a callback to process the data response by server
$.post("getstatus", {id:id}, function ( data ) {
data = $.parseJSON ( data ) // asume you use jQuery and the data is a json string
if ( data.result ){
state_2(); // if you want the result as a arg, do state_2( data.result )
return;
} else {
// no need to use finished flag, when there is a response and no result, call again
// anything when no success resulte you want to do could write here

state_1();
intervalCall = setTimeout ( updateStatus, 3000 );
}
});
},3000);
intervalCall = setTimeout ( updateStatus, 3000 );

一些更新

关于javascript - 用Javascript检查2个互斥条件的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10405972/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com