Tag: 信号量

javascript关键部分或信号量问题

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); […]

JavaScript或jQuery中的关键部分

我有一个网页,其中异步触发某个Ajax事件。 这个Ajax部分可以被调用一次或多次。 我无法控制触发此事件的次数,也无法控制时间。 此外,Ajax部分中有一些代码应作为关键部分运行,这意味着,当它运行时,不应该运行该代码的其他副本。 这是一个伪代码: 运行JavaScript或jQuery代码 输入作为Ajax的关键部分(当某个进程正在等待响应回调时,请不要再次输入此部分,直到此过程完成) 运行更多JavaScript或jQuery代码 我的问题是,如何按上述方式运行第2步? 如何使用JavaScript或jQuery创建/保证互斥部分。 我理解理论(信号量,锁,等等),但我无法使用JavaScript或jQuery实现解决方案。 编辑 如果你建议一个布尔变量进入临界区,这将不起作用,下面的行将解释原因。 关键部分的代码如下(使用布尔变量建议): load_data_from_database = function () { // Load data from the database. Only load data if we almost reach the end of the page if ( jQuery(window).scrollTop() >= jQuery(document).height() – jQuery(window).height() – 300) { // Enter critical section if (window.lock == false) […]