Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call a function in PHP after 10 seconds of the page load (Not using HTML)

Tags:

php

Is there any way to call a function 10 seconds after the page load in PHP. (Not using HTML.)

like image 588
Fero Avatar asked Aug 26 '09 09:08

Fero


People also ask

How do I run a function in PHP every 5 seconds?

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.

How do you call a function repeatedly after a fixed time interval in PHP?

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() .

How do you delay a function in PHP?

The sleep() function delays execution of the current script for a specified number of seconds.

Can I call a PHP function with onclick?

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.


2 Answers

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>
like image 142
Randell Avatar answered Oct 25 '22 17:10

Randell


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

like image 32
Fero Avatar answered Oct 25 '22 17:10

Fero