How to cancel Ajax queued up callbacks? (How to cancel long running method in Javascript?)

115 views Asked by At

I am kind of new to javascript.

This is not a website scenario, but android webview with local javascript and html, css ist still used.I am trying to figure out the total length of ebook, which comprises of n html documents.

What I am doing is I use n AJAX requests to request local file, which in the callback I append to the current document and calculate document length (via element.scrollWidth, which then seems to trigger the actual layout and can take several seconds -- for every request, heavy work)

Now I might need to cancel this whole operation.

BUT

As Ive learnt javascript uses event queue, so while AJAXes are parallel, the callbacks are synchronous. So in my case AJAXes are fast but callbacks take long. Here lies the problem. When I call my cancel() from android, that is just put onto the event queue, defacto after all the callbacks, so there is no way this cancel method can do anything to the already event loop queued up callbacks, like changing some cancel = true global variable, even aborting does nothing since all the ajaxed returned, just callbacks are waiting in the queue.

Is there a way to cancel this? Or do I need a totally differnt approach? (My guess is no, since in java that cancel method, and the processing would run on different threads, here in JS everyhing ran on single thread)

0

There are 0 answers