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?
- 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
4 Answers
Reset to default 5add 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...