I am never exactly sure when ajax-loaded.html is going to be generated (could be 1 second or 10 seconds), so I use setInterval to constantly call example_ajax_request(). That way when it does become available it shows up (within a second).
Question- How do I call myStopFunction() so that when example_ajax_request() actually does load ajax-loaded.html, setInterval stops?
<html>
<head>
</head>
<body>
<div id="example-placeholder">
<p>Placeholding text</p>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js" type="text/javascript"></script>
<script>
var myVar=setInterval(function(){example_ajax_request()},1000);
function example_ajax_request() {
$('#example-placeholder').html('<p><img src="ajax-loader.gif" /></p>');
$('#example-placeholder').load("ajax-loaded.html");
}
function myStopFunction(){
clearInterval(myVar);
}
</script>
</body>
</html>
You should probably use the load callback to clear the interval.
Polling every second is bad and You should use the
errorcallback to retry with a retryCount.