Page redirection does not work sequentially in PHP or JavaScript

I read various sources here and created the following ways to redirect the user in 10 seconds. Not used at the same time.

First try in PHP:

header("refresh:10;url=?switch=1") 

Then the second attempt in JavaScript:

 window.setTimeout(function () { location.href = '?switch=1'; }, 10000); 

They work almost all the time. They are used on the page using opens.js ( like this one ). Sometimes, when URL fragments change, the page is no longer redirected at all, although redirection most often does work.

Can someone tell me what could be the problem, or a good resource to read about the problem?

Edit: Switching to setInterval in the JavaScript version will re-awaken.

+5
source share
5 answers

When setting up headers with php, you must do this before creating any output, it is possible that you generate some kind of output in certain circumstances before the php header is set.

β€œIt's important to note that headers are sent when the first byte is output to the browser. If you replace the headers in your scripts, this means that placing echo / print statements and output buffers can actually affect the headers being sent.” - from http://php.net/manual/en/function.header.php You can find this page that will be useful for reading (if you have not passed it yet), I found it useful in encountering problems related to heading.

I would add more context (and related code) to your question if you want more detailed answers. We cannot know what is wrong with code that we do not see. If you include code that uses fragments of the URL that you install, we can better understand what your code is really doing, and may catch errors that you missed. Here are the Wicca fragments. just for completeness. https://en.wikipedia.org/wiki/Fragment_identifier

Finally, with the JavaScript example, I do not understand. It works great on my system. (although a reboot may take several microseconds). Could you use setInterval() instead?

+4
source

A quiz / memory game that moves from one question to another every ten seconds?

Rather, reloading the application every x seconds, create a one-page application that extracts data from your PHP script and then displays it every x seconds.

A very quick example (not verified):

 <!doctype html> <html> <head> <title>Example</title> <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> </head> <body> Question #<span id="question_number"></span><br /> <p id="question"></p> <script> var questionCounter = 0; function loadQuestion(questionNumber) { questionCounter++; $.get( "<your_php_script_url>?question="+questionCounter, function( data ) { $( "#question_number" ).html( questionCounter ); $( "#question" ).html( data.question ); setTimeout(function() { loadQuestion(questionCounter); }, 10000); }); } loadQuestion(questionCounter); </script> </body> </html> 
+14
source
  <!doctype html> <html> <head> <title>Example</title> <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> </head> <body> Question #<span id="question_number"></span><br /> <p id="question"></p> <script> var questionCounter = 0; function loadQuestion(questionNumber) { questionCounter++; $.get( "<your_php_script_url>?question="+questionCounter, function( data ) { $( "#question_number" ).html( questionCounter ); $( "#question" ).html( data.question ); setTimeout(function() { loadQuestion(questionCounter); }, 10000); }); } loadQuestion(questionCounter); </script> </body> </html> 

This is a good example, I also used this code.

+1
source

Try this simple example.

 setTimeout(function() { window.location='?switch=1' }, 10000); 
0
source

Use this jquery code

 window.setTimeout(function (){ window.location.replace("?name=Munish"); },10000); 
0
source

Source: https://habr.com/ru/post/1263024/


All Articles