0

Requirement is pretty simple, for some reasons I am unable to sort this out. Outer function calls inner function. Inner function is asynchronous, calls inner function on success if condition is matched. All fine - now user can run outer function while inner function is in progress. I want inner function to stop working immediately, and start afresh.

document.getElementById("demo").addEventListener("click", function()  {
 demo_click();
});
//
function demo_click() {
 var idnum = 0;
 var length = 25;
 innerFunc();
 //
 function innerFunc() {
  if (idnum < length) {
   console.log(idnum);
   setTimeout(function() {
    idnum++;
    innerFunc();
  }, 500);
  }
 }
}

It logs from 0 to 24 as expected. Here is fiddle - https://jsfiddle.net/h7ab8u69/2/

Solution might be trivial, but please suggest me what / where to put the condition.

EDIT

From start afresh I meant that previous calls of innerFunc would stop and it will start logging (console) from zero. It still starts from zero, but if you click twice, thrice and so on - logs (console) will appear from previous calls of innerFunc as well. Please visit above fiddle link and open console and click on button - problem will be evident.

PS

SetTimeout is just to display asynchronous call. In my code, which is Image.onLoad event. So, clearing the timeout is not an option. Basically, I am loading image one after another on button click. I want to stop previous loading and start new loading on another click. To make it simple, I used SetTimeout.

//var imgArray = [‘0.jpg’, ‘1.jpg’, ‘2.jpg’ and so on]
  function innerFunc() {
    var img = new Image;
    img.onload = function() {
      //draw this image on canvas
      //code to draw
      // draw next only if some condition is correct
      if (idnum < length) {
        idnum++;
        innerFunc();
      }
    };
    img.src = imgArray[idnum];
  }
Prasoon
  • 423
  • 9
  • 22
  • can you elaborate what does this phrase ` to stop working immediately, and start afresh` mean? (to prevent the first call ... or to postpone the function's call ?) – RomanPerekhrest Apr 06 '16 at 09:53
  • show your actual code with "Image.onLoad event" – RomanPerekhrest Apr 06 '16 at 10:16
  • Please investigate XHR object and `.abort` method as http://stackoverflow.com/questions/6929662/how-do-i-abort-image-img-load-requests-without-using-window-stop – Krzysztof Safjanowski Apr 06 '16 at 10:47
  • @RomanPerekhrest I got a chance to create a fiddle with images. Please have a look https://jsfiddle.net/yesprasoon/bammuga1/. Just resize browser (or change orientation in device mode) and you will be able to see multiple images. Please suggest. Feel free to update Fiddle. – Prasoon Apr 19 '16 at 07:52

1 Answers1

0

What i understood is you want to run some code and stop it when run the outer function again. If it is not timeout then you need to use a global variable.

var clickCount=0;

function demo_click() {
 var idnum = 0;
 var length = 25;
 clickCount++;
var thisCount = clickCount;
// stop the loop before starting again

 innerFunc();
  function innerFunc() {
    if (idnum < length) {
     console.log(idnum);



     for(var i =0 ; i < imgarraylength; i++){
       if(thisCount < clickCount){
         break;
       }
       idnum++;
       innerFunc();
     }
    }
  }
}

Hope it helps.

rajesh
  • 1,674
  • 1
  • 9
  • 15
  • Thanks and Sorry that I did not mention about SetTimeout usage in main post. That is just to mimic asynchronous call. I am not actually using SeTimeout in my code. Please see Edited post. – Prasoon Apr 06 '16 at 10:16
  • What do you mean by asynchronous call. Is it Ajax. If its ajax in that case you can abort the Ajax call too. – rajesh Apr 06 '16 at 10:19
  • Just noticed that you are loading image one by one. In that case before loading image you shoud check if one global variable if its true then only load. else break out of the loop. And when you cll the outer function you set that variable to true so that the existing looop will stop. – rajesh Apr 06 '16 at 10:22
  • Yes! that is exactly what I need, but unable to think of places to put variables. Could you please do the same (global check) in Fiddle? – Prasoon Apr 06 '16 at 10:29
  • 1
    Hey, check my edited answer. I have used a variable clickCount. I increment it when you click the button. So in the inner function you check whether the current value is less than the clickCount value. If ess then break out and dont run the image load. – rajesh Apr 06 '16 at 10:59
  • Almost there.. I can use this.. But it still calls setTimeOut from previous call one time (as the setTimeOut was already in process when I clicked) – Prasoon Apr 06 '16 at 11:05
  • I have removed set timeout now. using for loop and break to get out from that. Never hesitate to accept my answer if it helps :) – rajesh Apr 06 '16 at 11:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108389/discussion-between-prasoon-and-rajesh). – Prasoon Apr 06 '16 at 11:17
  • I got a chance to create a fiddle with images. Please have a look https://jsfiddle.net/yesprasoon/bammuga1/. Just resize browser (or change orientation in device mode) and you will be able to see multiple images. Please suggest. Feel free to update Fiddle. – Prasoon Apr 19 '16 at 07:51
  • 1
    I have made some changes and now it works fine. Please let me know if you still face that problem. https://jsfiddle.net/mishrarajesh/bammuga1/4/ – rajesh Apr 19 '16 at 17:31
  • With a quick test - it is working fine. I will test it tomorrow. Thanks for helping me till this point. I guess, best thing would be - I will add bits to my question, and then you modify your answer (in stead of comment) and then I will accept this as ANSWER. – Prasoon Apr 19 '16 at 19:49