473,408 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Delay PHP function w/o effect whole script.

1
Hello, i'm trying to set delay on function in php but i don't wanna use javascript or AJAX since i doesn't know how they function...
Anyway... i know about methods
Expand|Select|Wrap|Line Numbers
  1. sleep(1)
  2. usleep(1)
but i was wondering is it possible to delay execution of php script w/o effect on whole page loading...

Or is there a way to redirect a person who view the page and using
Expand|Select|Wrap|Line Numbers
  1. ignore_user_abort(TRUE);
and
Expand|Select|Wrap|Line Numbers
  1. sleep(1)
to don't delay the redirection but delay the page...

Thanks in advance, if you have any question... i will answer!
Jun 23 '13 #1
3 6651
Claus Mygind
571 512MB
You either load the page or don't load the page. If you try to delay partial loading of the page either the server or the client will timeout and the program will stop executing.

For what you want to do, you will have to do it in javaScript.
Jun 23 '13 #2
Atli
5,058 Expert 4TB
If the only goal is to execute a function some time after a web-site has been loaded, you can do that with a bit of trickery. The original script execution can't be delayed without holding up the client, but you can have that script set another script up to execute the function on a timer.

One cavity in this is that the following will only work on Linux hosts. I've got no idea if something equivalent exists for Windows, but I do know that this particular method does not work on Window.

OK, as an example, you could do this inside your main PHP script:
Expand|Select|Wrap|Line Numbers
  1. $time = time();
  2. shell_exec("php delayed.php {$time} >/dev/null&");
  3.  
This will trigger the "delayed.php" script to execute in the background, without it pausing the original PHP script while it finishes. Whatever happens in the "delayed.php" script happens completely separately.

So even if the original script redirects the client and exits, the delayed script will continue executing until it has finished it's job. You can use sleep functions there without any trouble:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (@$argc > 1 && (int)$argv[1] > 0) {
  3.     sleep(2);
  4.     require "inc/functions.php";
  5.     doDelayedStuff((int)$argv[1]);
  6. }
  7. else {
  8.     error_log("Invalid time parameter passed to the delayed script.");
  9. }
  10.  
This will execute some function 2 seconds after it is called, passing it the time value passed to the script. (That whole time thing is just an example of how you could pass values between the original script and the delayed script.)
Jun 23 '13 #3
Luuk
1,047 Expert 1GB
Or is there a way to redirect a person who view the page and using
use $_SERVER['REMOTE_HOST'], or some other way to detect 'a person', and send him to some other place....
Jun 23 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: gmv | last post by:
Hi, I am doing a project and i would like to know if anyone can please tell me how to write a delay or wait function in c++ which works asynchronously with other programs. Thank you, GMV
2
by: Hari Sekhon | last post by:
I want to wrap a whole script in try ... except. What is the best way of doing this? Consider the following: - try: import <modules> <CODE>
24
by: Chen Shusheng | last post by:
Hello, I want to write a time delay function like "Timedelay(float time_lenth){}". When execute it, it will delay some seconds as long as "time_lenth" indicating. Could you help on how to write...
1
by: rameshs | last post by:
hello, for me, I want to get a SSID (name) from a function when click a button and display it. I am using " cliSet.c " C file for storing that name. In that C file I am using nameSet()...
3
by: datttanand | last post by:
How to write the main function in java script? such as in vb script sub main end sub
7
by: agsupriya86 | last post by:
Hi, I need to add some delay function to my code where on clicking the exit button some text needs to be displayed on the text area.. Right now the text is not not appearing coz there s no dealy...
1
by: klmishraa79 | last post by:
i want to know how i can put a time delay between two command execution...i.e. after first command of programm the second command should execute after some fixed delay......i want to use time delay...
8
rpnew
by: rpnew | last post by:
HI, I'm working with PHP/MySql... on one page i've used this XMLHttprequest object. Now i'm developing the system on FC4/FireFox.. on my machine it works fine but if i try to access it from...
1
by: maul581 | last post by:
I know the delay( ) function of Turbo C and used it in my project. By this function we can give intermediate delay in execution from 1 millisecond to 9999 millisecond. My main question is "Is it...
3
Ali Rizwan
by: Ali Rizwan | last post by:
Hello all, I want to put delay in my loops as we use in c/c++.. Is there any delay function in vb6 which put delay between any code??? Thanx >> ALI <<
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.