function myobj(){
var gup=this;
this.lastindex=-1;
this.criticalSectionInTimer=0;
this.updateTimer;
this.start = function(l){
if((typeof this.updateTimer)=="number"){
clearInterval (this.updateTimer);
}
this.updateTimer=setInterval(function() {gup.getMessages();} , 30);
}
this.stop= function(){
if((typeof this.updateTimer)=="number"){
clearInterval (this.updateTimer);
}
}
this.addUpdate(i){
//some code
}
this.rrrrnr=0;
this.getMessages = function(){
if(this.criticalSection==0){
this.criticalSection=1;
this.rrrrnr++;
console.log("in critical section"+this.rrrrnr);
var url="getmessages.php?lastindex="+this.lastindex;
$.getJSON(url,
function(data){
gup.lastindex=data.lastindex;
$.each(data.updates, function(i,item){
gup.addUpdate(item);
});
}
);
console.log("out critical section"+this.rrrrnr);
this.criticalSection=0;
}
}
}
var m= new myobj();
myobj.start();
我有上面的代码。我有一个主循环在给定的时间间隔进行更新。问题是我已经意识到它已经进入了我已经由变量this.criticalSection定界的“临界区”。javascript临界区或信号量问题
从firebug我得到的消息“在关键部分”+索引和“出关键部分”+索引在正确的顺序,但ajax请求仍在处理中。但我得到了同样索引的请求,我真的不知道该在哪里寻找问题。
JavaScript中是否有信号量或关键部分的任何buildin功能?
使用同步ajax调用通常是一个糟糕的选择。 UI在请求期间被完全锁定并显示为冻结。 –
@Jonathon我发现与UI锁定相同的问题;( – vaske