Is there any way to call a function 10 seconds after the page load in PHP. (Not using HTML.)
React PHP is a widely used event loop for PHP. define("INTERVAL", 5 ); // 5 seconds function runIt() { // Your function to run every 5 seconds echo "something\n"; } function checkForStopFlag() { // completely optional // Logic to check for a program-exit flag // Could be via socket or file etc. // Return TRUE to stop.
setInterval() The setInterval() method, offered on the Window and Worker interfaces, repeatedly calls a function or executes a code snippet, with a fixed time delay between each call. This method returns an interval ID which uniquely identifies the interval, so you can remove it later by calling clearInterval() .
The sleep() function delays execution of the current script for a specified number of seconds.
Given a document containing HTML and PHP code and the task is to call the PHP function after clicking on the button. There are various methods to solve this problem. Also, apart from doing this with the click of a button, a PHP function can be called using Ajax, JavaScript, and JQuery.
PHP is a server side scripting language. If you need to check if something has loaded already in the client side, you will need a client-side scripting language like JavaScript.
You might need to use jQuery for your purpose to simplify things.
jQuery is a slow JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript.
First, download jQuery. In the head tag of your HTML, add this:
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
// Check if the page has loaded completely
$(document).ready( function() {
setTimeout( function() {
$('#some_id').load('index.php');
}, 10000);
});
</script>
In the body of your HTML, add this:
<div id="some_id"></div>
This code works. Edited from randell's answer.
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
setTimeout(function() { $('#some_id').load('index.php'); }, 10000);
});
</script>
Thanks to randell
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