最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to lock AJAX functions from overlapping? - Stack Overflow

programmeradmin1浏览0评论

I've got one function checkEvery15Seconds that runs every 15 seconds. It checks to see if new ments have been added to a page.

I've got a form that submits a new ment once the submit button is pressed, then displays the new ment on the page.

In the process of adding a new ment checkEvery15Seconds is querying the database at the same time, so I end up with duplicate ments on the page (not in the database though, this is purely a JavaScript issue).

How can I get my "submitComment" function to stop checkEvery15Seconds and restart it after the "submitComment" function has finished executing?

I've got one function checkEvery15Seconds that runs every 15 seconds. It checks to see if new ments have been added to a page.

I've got a form that submits a new ment once the submit button is pressed, then displays the new ment on the page.

In the process of adding a new ment checkEvery15Seconds is querying the database at the same time, so I end up with duplicate ments on the page (not in the database though, this is purely a JavaScript issue).

How can I get my "submitComment" function to stop checkEvery15Seconds and restart it after the "submitComment" function has finished executing?

Share Improve this question edited Feb 25, 2015 at 16:05 Martin Thoma 137k173 gold badges679 silver badges1k bronze badges asked Mar 9, 2010 at 23:24 AndrewAndrew 240k195 gold badges531 silver badges718 bronze badges 1
  • As an alternate solution, just make sure you don't add the ment to the page if it's already there, by the ment ID. Make the HTML ment container you add have an id "ment_N" and then check for that element before you add a new one. – Nicole Commented Mar 9, 2010 at 23:28
Add a ment  | 

4 Answers 4

Reset to default 5

add a boolean called somewhat suspend15sCheck in a scope which is accessible by both functions. enable it while adding the ment and afterwards set it to false again.

in your 15sCheck-function you first have to check if you are allowed to check :-)

var suspend15sCheck = false;

function addComment()
{
  suspend15sCheck = true;

  // add ment on base of form data

  suspend15sCheck = false;
}

function myTimer()
{
    if(suspend15sCheck === false) 
    {

      // add ments via ajax request
      // remember to check if the ments who will be added already exist :-)

    }
}

Simplest solution: use a flagging variable that you turn on and off. The first line of your "checkEvery15Seconds" function reads: if (!global_checkingEvery15Seconds) return;

Just set that variable (whatever you name it, global or object-bound) to true when you want the checking turned on, and off when you don't.

You'll need a status variable to indicate the current state of the ment ajax request

var requestComments = false;

if(requestComments === false) {
  requestComments = true;
  // make ajax request

  // on ajax success/fail
  requestComments = false;
}

Wrap it up in an object that allows other functions to set start/stop flags on it.

function My15SecondsObj() {
    var objSelf = this;

    // 
    this.run();
}

My15SecondsObj.Paused = false;
My15SecondsObj.prototype.run= function() { 
   if (!Paused)
   {
      // Do your call here
   }
   var _this = this;
   setTimeout(function() { _this.run(); }, 15000);

}

Now when you want to use this object, just do

var myObj = new My15SecondsObj();

and when you want to pause it,

myObj.Paused = true;

and start it again by doing:

myObj.Paused = false;

Add some events if you want to get really crazy, so that other objects can subscribe to notifications about when the database updates have succeeded, etc...

发布评论

评论列表(0)

  1. 暂无评论