I am calling ajax after each 5 secs to update my data and I need to stop that call when a button is clicked.
My ajax function is:
function get_text(){
var text = $.ajax({
type: "POST",
url: "getIt.php",
async: false
}).complete(function(){
setTimeout(function(){get_text();}, 5000);
}).responseText;
$('#editor_Content').html(text);
}
this function is called at
$(document).ready(function(){
new get_text();
}
I need to stop this call when a button is clicked and start this call again when the same button is clicked again. Any suggestions?
My button click code is
function editit(){
var myVar = document.getElementById("editor_Content").getAttribute("contenteditable");
if(myVar=='true'){
document.getElementById("editor_Content").setAttribute("contenteditable", "false");
document.getElementById("editbtn").setAttribute("value","Edit Text");
} else{
document.getElementById("editbtn").setAttribute("value","Done Editing");
document.getElementById("editor_Content").setAttribute("contenteditable", "true");
}
}
You need to save the result of
setTimeoutand use it in a call to theclearTimeoutfunction like so:But for your purposes I think
setIntervalandclearIntervalwould work better. Here is what your new code would look like: