The issue is that the ajax call is asynchronous. JavaScript puts this makes the request to the server and then continues on to the next line of code. Your success and error callback functions are not being called until after JavaScript has completed execution of your while loop.
Furthermore, the structure of the code doesn't make any sense at all (sorry, not trying to be rude). The $canAppend and $iPage variables are not used or changed. What this code will do is enter into a loop and never exit. Why is this? It's because the call to $.ajax() is non blocking. It won't wait there until the request is completed, it will continue on. Since JavaScript is (essentially) single threaded the callbacks for error and success CAN'T be executed until the current execution process is completed. Because the success and error handlers can't be run the $sid can't be set. Because $sid can't be sent the code can't exit the while loop.
I don't see how you code is actually making use of the while loop. Instead, just call the $.ajax() function and handle the results in your success handler. Try this on for size to help you better understand what's going on:
$sid = 200;
alert("$sid is 200: " + $sid); // you'll see this first
$.ajax({
url: "tmp/result1.html",
success: function(html)
{
if(html)
{
$("#inzeraty").append(html);
$('div#loadmoreajaxloader').hide();
$sid = 0;
alert("$sid is now 0: " + $sid); // you'll see this third if html is not false
} else {
alert("$sid is STILL 200: " + $sid); // you'll see this third if html is false
}
},
error: function()
{
$sid = 0;
alert("you got an error, but $sid is now 0: " + $sid); // you'll see this third if there's an error
}
});
alert("$sid is still 200: " + $sid); // you'll see this second