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 分隔的“关键部分”。
从萤火虫我得到消息“在关键部分”+索引和“关键部分”+索引以正确的顺序,但ajax请求仍在处理中。但是我收到具有相同索引的请求,我真的不知道在哪里寻找问题。
javascript 中的信号量或关键部分是否有任何内置功能?