I have a .php script that I use for creating the list of my products. I am on shared hosting, so I can”t do a lot of queries otherwise I get a blank page.

Đang xem: Php, wait 5 seconds before executing an action

This is how I use my script now:

script.php?start=0&end=500&indexOfFile=0 ->> make a product0.txt file with first 500 products script.php?start=501&end=1000&indexOfFile=1 ->> product1.txt file with another 500 products script.php?start=1001&end=1500&indexOfFile=2 ->> product2.txt file with last 500 products

How can I modify the script so it will make all these files automatically, so that I don”t have to change each time the link manually?

I would like to click a button which will do this:

make the product0.txt file with the first 500 products

wait 5 seconds

make the product1.txt file with with another 500 products

wait 5 seconds

make the product2.txt file with the last 500 products

php
Share
Improve this question
Follow
edited Jan 19 “18 at 12:25
Hamza Zafeer
2,032 12 12 gold badges 25 25 silver badges 34 34 bronze badges
asked Jul 18 “11 at 9:23
user849690user849690
401 1 1 gold badge 4 4 silver badges 3 3 bronze badges
2
10
In less than the time it took you to write this, you could have copied the contents of script.php, pasted it twice more, hardcoded the $start and $end values in each section, and added a sleep(5) between each copy/paste. That's the whole script. – Dan Grossman Jul 18 “11 at 9:26
best way I think is to retrieve all the products you need in a global array then slice it, write data into separate files and you're done.This only need one sql query – krifur Jul 18 “11 at 9:29
Add a comment  | 

7 Answers 7

Active Oldest Votes
72

use:

sleep(NUMBER_OF_SECONDS);
Share
Improve this answer
Follow
answered Jul 18 “11 at 9:26
BrianBrian
7,606 2 2 gold badges 22 22 silver badges 32 32 bronze badges
Add a comment  | 
32

before starting your actions, use

sleep(5);
Share
Improve this answer
Follow
answered Jul 18 “11 at 9:27
genesisgenesis
48.2k 18 18 gold badges 90 90 silver badges 118 118 bronze badges
Add a comment  | 
13

or:

usleep(NUMBER_OF_MICRO_SECONDS);
Share
Improve this answer
Follow
answered Jul 18 “11 at 9:49
Lawrence CheroneLawrence Cherone
41.1k 7 7 gold badges 48 48 silver badges 90 90 bronze badges
Add a comment  | 
2

In Jan2018 the only solution worked for me:

<?php if (ob_get_level() == 0) ob_start(); for ($i = 0; $i<10; $i++) echo “Done.”; ob_end_flush();

?>

Share
Improve this answer
Follow
answered Feb 21 “18 at 20:33
Ari WaisbergAri Waisberg
879 1 1 gold badge 9 9 silver badges 20 20 bronze badges
Add a comment  | 
1

i use this

$i = 1; $last_time = $_SERVER<"REQUEST_TIME">; while($i > 0) }

you can use

function WaitForSec($sec) } }

and run code =>

WaitForSec(your_sec);

Example :

WaitForSec(5);

OR you can use sleep

Example :

sleep(5);
Share
Improve this answer
Follow
answered May 12 “20 at 18:24
Khaled DeveloperKhaled Developer
21 1 1 bronze badge
3
Why writing a custom function if there is a built in one that performs the same function ? Someone may down vote your answer for this …. – Szekelygobe Jun 11 “20 at 20:13
My shared hosting prohibits / disables php sleep(). Thanks
kha – somebadhat Feb 5 at 17:24
kha $_SERVER<'REQUEST_TIME'> produced an infinite loop on my shared hosting. I switched to $now = time(); while ($now + 4 > time()) – somebadhat Feb 5 at 18:34
Add a comment  | 
0

I am on shared hosting, so I can”t do a lot of queries otherwise I get a blank page.

That sounds very peculiar. I”ve got the cheapest PHP hosting package I could find for my last project – and it does not behave like this. I would not pay for a service which did. Indeed, I”m stumped to even know how I could configure a server to replicate this behaviour.

Regardless of why it behaves this way, adding a sleep in the middle of the script cannot resolve the problem.

Xem thêm: Chẩn Đoán Ung Thư Tinh Hoàn : Nguyên Nhân, Triệu Chứng, Ung Thư Tinh Hoàn: Nguyên Nhân, Triệu Chứng

Since, presumably, you control your product catalog, new products should be relatively infrequent (or are you trying to get stock reports?). If you control when you change the data, why run the scripts automatically? Or do you mean that you already have these URLs and you get the expected files when you run them one at a time?

Share
Improve this answer
Follow
answered Jul 18 “11 at 13:11
symcbeansymcbean
45.2k 5 5 gold badges 48 48 silver badges 83 83 bronze badges
1
1
he may be getting blank results because he's not freeing up results after executing each query – b_dubb Jun 29 “15 at 16:57
Add a comment  | 
-5

In https://www.php.net/manual/es/function.usleep.php

<?php // Wait 2 seconds usleep(2000000); // if you need 5 seconds usleep(5000000); ?>
Share
Improve this answer
Follow
edited Mar 15 “20 at 22:42
answered Aug 11 “17 at 13:23
DarwinDarwin
32 1 1 silver badge 7 7 bronze badges
1
4
foreach is unnecessary,and delay isn't a function in PHP that I'm aware of. – MSC Oct 20 “17 at 19:21
Add a comment  | 

Your Answer

Thanks for contributing an answer to Stack Overflow!

Please be sure to answer the question. Provide details and share your research!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Xem thêm: Nạp Thẻ Viettel Liên Quân – Cách Nạp Tiền Trong Liên Quân Chi Tiết Nhất

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Submit

Post as a guest

Name
Email

Required, but never shown

Post as a guest

Name
Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged php or ask your own question.

The Overflow Blog
State of the Stack: a new quarterly update on community and product
Podcast 320: Covid vaccine websites are frustrating. This developer built a…
Featured on Meta
State of the Stack Q1 2021 Blog Post
Visit chat
Linked
2
Delay unlink after Header
Related
2773
How can I prevent SQL injection in PHP?
1196
Enumerations on PHP
2699
Deleting an element from an array in PHP
1344
How do I make a redirect in PHP?
1558
startsWith() and endsWith() functions in PHP
1817
How do I get PHP errors to display?
2179
How do you parse and process HTML/XML in PHP?
4679
Reference — What does this symbol mean in PHP?
2103
How does PHP 'foreach' actually work?
2565
Why shouldn't I use mysql_* functions in PHP?
Hot Network Questions
The best way to say Sinners
Got a weird trans-purple cone part as extra in 71043-1 Hogwarts Castle
What is the best 2nd level spell to concentrate on to maximize damage?
Idiom similar to “if the shoe fits”
Do I have to relinquish my sign on and passwords for websites pertaining to work (ie: access to insurance companies and medicare)?
Photo Competition 2021-03-29: Transportation
Have any kings ever been serving admirals?
What is the meaning of "Cancel Culture"?
Does making an ability check take an action?
Identify this tool
Looking for a measure of variance of variance?
Boom of the Thunderbird
Could sapient amphibians develop metallurgy?
What does "But… They went home" mean in Maya Angelou's "They Went Home"?
Is this a draw despite the Stockfish evaluation of −5?
What does the Gemara mean?
Can a broken egg spontaneously reassemble itself (as in the video)?
Calculating mass expelled from cold gas thrusters
Translation of lucis mortiat / reginae gloriae
Is there any official/semi-official standard for music symbol visual appearance?
How can I extract the contents of a Windows 3.1 (16-bit) game EXE file?
How does moving silently with Greater Invisibility after taking an action work?
How can I draw the trefoil knot in 3D co-ordinates in Latex using these parametric equations?
What is the best way to turn soup into stew without using flour? more hot questions
Question feed
Subscribe to RSS
Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-php
Stack Overflow Questions Jobs Developer Jobs Directory Salary Calculator Help Mobile
Products Teams Talent Advertising Enterprise
Company About Press Work Here Legal Privacy Policy Terms of Service Contact Us Cookie Policy
Stack Exchange
Network Technology Life / Arts Culture / Recreation Science Other
Stack Overflow Server Fault Super User Web Applications Ask Ubuntu Webmasters Game Development
TeX – LaTeX Software Engineering Unix & Linux Ask Different (Apple) WordPress Development Geographic Information Systems Electrical Engineering
Android Enthusiasts Information Security Database Administrators Drupal Answers SharePoint User Experience Mathematica
Salesforce ExpressionEngine® Answers Stack Overflow em Português Blender Network Engineering Cryptography Code Review
Magento Software Recommendations Signal Processing Emacs Raspberry Pi Stack Overflow на русском Code Golf
Stack Overflow en español Ethereum Data Science Arduino Bitcoin Software Quality Assurance & Testing Sound Design
Windows Phone more (28)
Photography Science Fiction & Fantasy Graphic Design Movies & TV Music: Practice & Theory Worldbuilding Video Production
Seasoned Advice (cooking) Home Improvement Personal Finance & Money Academia Law Physical Fitness Gardening & Landscaping
Parenting more (10)
English Language & Usage Skeptics Mi Yodeya (Judaism) Travel Christianity English Language Learners Japanese Language
Chinese Language French Language German Language Biblical Hermeneutics History Spanish Language Islam
Русский язык Russian Language Arqade (gaming) Bicycles Role-playing Games Anime & Manga Puzzling
Motor Vehicle Maintenance & Repair Board & Card Games Bricks Homebrewing Martial Arts The Great Outdoors Poker
Chess Sports more (16)
MathOverflow Mathematics Cross Validated (stats) Theoretical Computer Science Physics Chemistry Biology
Computer Science Philosophy Linguistics Psychology & Neuroscience Computational Science more (10)
Meta Stack Exchange Stack Apps API Data
Blog Facebook Twitter LinkedIn Instagram

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.3.12.38768

Stack Overflow works best with JavaScript enabled

*

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *