-2

I'm trying to make a website load forever. My current idea is to request a PHP file:

<?php
sleep(30);

This will delay the load by 30 seconds, which a quick Google search tells me should be within most browsers' timeouts. I was thinking of writing some JavaScript to append a new link tag after a bit less than 30 seconds to keep the page loading, but I found that this didn't keep the loading icon spinning (with Chrome at least):

window.addEventListener( 'load', () => {
    var i = 0;
    setInterval( () => {
        i++;
        var newScript = document.createElement('script');
        newScript.src = 'infinite-loading.php?i=' + i;
        document.querySelector('#infinite-loading').after(newScript);
        console.log('The deed is done');
    }, 25000)
} )
<script id="infinite-loading" src="infinite-loading.php"></script>

The code above appends a script tag every 25 seconds, and the browser loads the PHP file each time, but it doesn't show the loading icon. I added the URL parameter because I wasn't sure if browsers would cache the page.

I also want to make sure that the server with the PHP file won't be overloaded. I'm not sure if many sleep() functions running constantly at the same time will cause any issues.

Is there a better way to do this client-side? Should I use something other than PHP? Something multi-threaded?

(Edit: Sorry for the awkward title, Stack Overflow didn't like my first one.)

jxxe
  • 234
  • 1
  • 4
  • 12
  • What you are trying to do doesn't make sense. Maybe what you want to do is use websockets, so that you can communicate at any time. You need to allow the page to load completely, and then you can update things when needed. – Mikkel Sep 05 '20 at 07:03
  • @Mikkel I'm not doing this for any practical purpose. It's supposed to be a joke, because it's annoying when websites take forever to load. In my case, I want it to keep spinning even when the content is loaded. – jxxe Sep 05 '20 at 07:08

2 Answers2

1

You need that browser will continue reading your page forever (I'm talking about HTML, not other linked objects). So you need not to break timeout and feed some data from backend to frontend.

Example of sending portion of data to client:

ob_end_flush();

# CODE THAT NEEDS IMMEDIATE FLUSHING

ob_start();

Now we need to understand the minimum data packet size that is expected by the browser. Minimal googling tells us a limit of 8-10 bytes.

So combining this together we can try to check (I did not checked, it is just my version):

<?php
while (true) {
  sleep(25);
  ob_end_flush();
  echo "          "; // 10 spaces...
  ob_start();
}
Anton
  • 2,669
  • 1
  • 7
  • 15
  • This seems to work pretty well. I had no idea `ob_end_flush()` and `ob_start()` existed. Is this bad for performance from the server? – jxxe Sep 05 '20 at 07:13
  • @jxxe in this situation where you want to hang up the connection, it is not a problem. I think it will not lower the performance. – Anton Sep 05 '20 at 09:47
-1

Not sure why you would want to do anything like this but the simplest solution I think is an endless loop.

<?php

while(true)
{

}