Writing a dynamic data stream in a div:
Here we are talking. You specifically asked how to dynamically record data streams in a "div". As many have said, you can dynamically write in iframes, and we just need to take it one step further. Here is a complete solution to your problem that will return this data to your div with a maximum delay of 0.5 seconds. It can be adapted if you need a faster update.
<!DOCTYPE html> <html> <head> <title>dynamic listener</title> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <script type="text/javascript"> var count; $(function(){ $('#formx').submit(function(){ setTimeout(function(){ check_div(); }, 500); count = 0; return true; }); }); function check_div() { var $iframetxt = $('#iframex').contents().text(); var $div = $('#dynamic'); if( $iframetxt != $div.text() ) { console.log('rewritten!'); $div.text( $iframetxt ); setTimeout(function(){ check_div(); }, 500); count = 0; } else { count++; if(count < 40) setTimeout(function(){ check_div(); }, 500); else console.log('timed out'); } } </script> </head> <body> <div> Form <form id="formx" action="result.php" method="post" target="iframex"> <input type="submit" /> </form> </div> <div id="dynamic"></div> <iframe id='iframex' name="iframex" style="display:none" ></iframe> </body> </html>
1. In the submit form, streaming data is sent in an iframe.
To do this, we simply set the target attribute in the form tag to the iframe name.
2. check_div () runs every 0.5 seconds to compare the text of the #dynamic div with the text content of the iframe.
If there is a difference between them, the data is written to the div, and the timeout is called again. If there is no difference, the timeout counter is incremented. If the counter is less than 40 (40 x.5 s = 20 seconds), it again calls the timeout. If not, we assume the thread is complete.
James L.
source share