I've made an ajax call with a jQuery.everyTime() function. I got a combo box where i select some graph names and dynamically calls to an ajax function, returns a json and creates the chart in the View every 10 seconds.
Everything goes fine but when i select another graph name and click in the function, i don't only have the new graph but i got the old one as well (as a request), so every time i click in a new one (let's say 8 names) i would get 8 requests simultaneously and ofc the latest will be shown (but if you check in firebug you will see the 8 requests).
This is my ajax function:
var selected = $("#name_list :selected").val();
$(".title_graph").text(selected);
var j = jQuery.noConflict();
j("#hour").everyTime(10000,function(i){
j.ajax({
type: "GET",
url: "getchartdata?graphName=" + selected +"&subgroup=hour",
cache: false,
success: function(jsonData){
var data = eval(jsonData);
drawChart(data, data[0][0], data[0][1]);
}
})
});
I would like to cancel previus ajax calls without having to refresh the page. Am i able to do that? like put some kind of "stop" at the very beginning of the function, don't really know. I've seen ajaxName.abort() solution, but i believe it couldn't be applied to what i need.
Thanks in advance.
ADDED:
This is how it looks now with Travis' suggestion:
function getChartsByGraphName() {
var selected = $("#name_list :selected").val();
var ajaxCallHour;
$(".title_graph").text(selected);
var j = jQuery.noConflict();
j("#hour").everyTime(10000,function(i){
ajaxCallHour && ajaxCallHour.abort();
ajaxCallHour = j.ajax({
type: "GET",
url: "getchartdata?graphName=" + selected +"&subgroup=hour",
cache: false,
success: function(jsonData){
var data = eval(jsonData);
drawChart(data, data[0][0], data[0][1]);
}
})
});
}
But it's still sending old ajax requests.