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 변수에 의해 구분 된 "중요한 섹션"에 들어 있다는 것을 깨달았습니다.자바 스크립트 중요 섹션 또는 세마포 문제
firebug에서 "critical section"+ index와 "out critical section"+ 올바른 순서로 색인을 얻지 만 Ajax 요청은 여전히 처리 중입니다. 그러나 나는 같은 지수로 요청을 받고, 나는 정말로 그 문제를 찾을 곳을 모른다.
자바 스크립트에서 세마포어 또는 중요 섹션에 대한 빌드 인 기능이 있습니까?
동기식 ajax 호출을 사용하는 것은 일반적으로 좋지 않습니다. UI가 완전히 잠겨져 요청 중에 고정되어 나타납니다. –
@Jonathon UI 잠금과 동일한 문제가 있음 ( – vaske