I am trying to create a script to continue to work even if it fails until a specific word is returned. The bottom function will work for about 30 seconds, it will stop because of the PHP script (this is what I want), and then jquery will make it run again and again (until a specific trigger is returned). Top function get_data (); will be executed every one and a half seconds after it completes the request (it basically finds out what stage the other request is at.
My problem is to maintain the order of things, when the request is activated again, it starts the function again get_data()
, which is not needed, if in this case there is no error. To overcome this, I just wanted to complete the whole ajax request and start again (the get data function works only with a small PHP script, which will take half a second with max and about 10 seconds with the other, so it wonβt do much harm by doing this) but I can't get it to stop ajax requests, I just keep sending multiple requests get_data()
in all directions ...
$(document).ready(function() {
var number = 0;
var requests = [];
function kill_requests( requests ){
$.each( requests, function( i, v ){
v.abort();
})
}
function get_data( url, requests ){
$('#requests').html( number );
requests.push( $.ajax({
type: 'POST', url: './scrape/ajaxGetData.php', data: 'url=' + encodeURIComponent(url), cache: false, timeout: 10000,
error : function(){ },
success: function(html){
if( html.substr(0,12) == '<!-- die -->' ) {
kill_requests( requests );
$("#result").html('<p>Complete...</p>' + html );
}else{
$("#result").html(html);
window.setTimeout( function(){ number++; get_data( url, requests ) }, 2000 );
}
}
}));
}
$("input[name=submit]").live( "click", function(){
var url = $("input[name=url]").val();
requests.push( $.ajax({
type: 'POST', url: './index.php', data: 'submit=true&url=' + encodeURIComponent(url), cache: false,
error : function(){
kill_requests( requests );
$("input[name=submit]").trigger('click');
},
success: function(html){
$("#result2").html(html);
if( html.substr(0,12) != '<!-- die -->' ) {
kill_requests( requests );
$("input[name=submit]").trigger('click');
}
}
}));
get_data( url, requests );
});
});
source
share