0

I have a photos.php page that is getting called dynamically with the help of jquery load function as below :

 $("#photo_gallery").load("./photos.php"

However I want a way to manually stop the loading of photos.php if the user clicks another button exactly after the above function call .

for example if the user clicks a button that triggers $("#photo_gallery").load("./photos.php") and after exact next moment,if the user clicks another button,then the above loading should immediately stopped.Appreciate if anyone can help me here.

     $("#photo_gallery).empty() will not work here (it doesn't work while the php page loads,it only works when after/before the load)
Lkopo
  • 4,798
  • 8
  • 35
  • 60
user3905438
  • 441
  • 1
  • 4
  • 6
  • You could recursively call your load function and have a condition before anything loads, a button could toggle the variable so that you could stop and start this loading function – Halfpint Sep 09 '14 at 18:52
  • Duplicate of http://stackoverflow.com/questions/2813168/how-to-cancel-a-jquery-load – Charlotte Dunois Sep 09 '14 at 18:55

1 Answers1

0

In extension to my comment you could have something like this

var load = true;

$(document).ready(function() {
     load_images(load);
});


// Recursively call the load function as long as toggle is true
function load_images(toggle) {
    if(toggle) {
        $("#photo_gallery").load("next_image");
        load_images(true);
    }
}

// Toggle load button
$('#toggle_load').on("click", function() {
     if(load) {
           load = false;
           load_images(load);
     } else {
           load = true;
           load_images(load);
     }
});
Halfpint
  • 3,967
  • 9
  • 50
  • 92
  • may also be helpful to add an interval/timeout in there too of around 200ms, otherwise its likely all your images will load in very quickly - you should also do some checks to see if you have reached your last image aswell, just wanted to give you a guideline :) – Halfpint Sep 09 '14 at 19:08