$(document).ready(function() {
$("#test").click( function(){
test(0,20);
});
$("#stop").click( function(){
// как остановить test();
});
});
test = function (i,max){
if(i <= max){
$.ajax({
type: 'POST',
url: 'in.php',
data: {x:'user'},
dataType: "text",
cache: false,
success: function(dat) {
console.log(i);
test(i+1,max);
}
});
}
}
-
Таким образом остановить не получится, т.к. нет условий для выхода, которыми можно управлять извне.– Alex Krass17 янв 2014 в 4:37
Добавить комментарий
|
2 ответа
Как вариант завести глобальную переменную (или переменную в объекте test) по значению которой будет вызываться функция test или нет. В событии где надо остановить, просто изменять это значение.
$("#stop").click( function(){
stopFun = true;
// как остановить test();
});
success: function(dat) {
console.log(i);
if (!stopFun) {test(i+1,max)};
}
var $ajaxObj = null;
var stop = false;
$(document).ready(function() {
$("#test").click( function(){
test(0,20);
});
$("#stop").click( function(){
$ajaxObj.abort();
stop = true;
});
});
test = function (i,max){
if(i <= max && !stop){
$ajaxObj = $.ajax({
type: 'POST',
url: 'in.php',
data: {x:'user'},
dataType: "text",
cache: false,
success: function(dat) {
console.log(i);
if(!stop) test(i+1,max);
}
});
}
}