my task is to do a long running JS code. This routine generate a POST request, and when its over and need to, generates another according to the answer:
function routine (a)
{
var answer = createPostRequest (bla bla bla);
if (answer)
{
routine (a);
}
}
so it recursively calls itself as long as the answer is true. So far its good, but then browser freezes, or hangs too much. After a time, Firefox will tell me that script is running too long, and offers to stop it.
Instead of doing routine (a); I tried to do with a setTimeout with timing 1. The same things, but when I set 100 for timing, it looks ok. But there are unnecessarry waitings, plus its a subjective number (what if even that 100 causes problems?)
I need some kind of “message based” thing, like in Delphi/Windows programming: a program sends a message to itself. How can it be achived in JS?
Edit: the way I generating the request:
var ajax = createXmlHttp();
ajax.open ('POST', 'dsdsdsad.adsf', false);
var parameters = 'param=1';
ajax.setRequestHeader ('Content-type', 'application/x-www-form-urlencoded');
ajax.setRequestHeader ("Content-length", parameters.length);
ajax.setRequestHeader ("Connection", "close");
ajax.send (parameters);
try
{
var answer = eval('(' + ajax.responseText + ')');
}
catch (error)
{
alert ('Error in the answer: '+ajax.responseText);
return;
}
I would assume your
createPostRequestmakes an AJAX call that can handle a callback.If so, make sure the request is asynchronous and pass it a callback that tests the condition and makes the next call if needed.