This is my first question, and I would appreciate you answering soon.
I would like code to repeat a function continuously... I have tried some code but it hasn't worked.
I tried this code:
<script type="text/javascript">
$(function()
{
$('#more').load('exp1.php'); // SERIOUSLY!
});
</script>
I want to repeat this function after some interval. I have tried
setInterval()
and setTimeout()
But, I haven't received results.
This will repeat the task until you clear the interval (with clearTimeout(repater))
var repeater;
function doWork() {
$('#more').load('exp1.php');
repeater = setTimeout(doWork, 1000);
}
doWork();
Per OP's original condition:
I want code that repeat function continuously...
you can do this like
var myFunction = function() {
$('#more').load('bla.php');
};
var timer = setInterval(myFunction, 1000); // call every 1000 milliseconds
or
var timer = setTimeout(myFunction, 1000); // call every 1000 milliseconds
clearTimeout(timer); //To stop the function from being called forever
as @Christofer Eliasson For an Ajax-request, you would probably want to use a timeout instead of an interval, an start the timeout again in the callback, to make sure that you don't stack calls if the server is taking more than 1 second to respond
Good Read
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With