javascript - Why won't my script update my div dynamically like it's supposed to? -
<html> <head> <style type="text/css"> body { background-color: #000000; font-family: arial, helvetica, sans-serif; font-size: 20px; text-align: center; color: #ffffff; } </style> <script type="text/javascript" src="jquery-1.4.js"></script> <script type="text/javascript"> function sleep(ms) { var dt = new date(); dt.settime(dt.gettime() + ms); while (new date().gettime() < dt.gettime()); } function update() { while (0 < 1) {<?php $contents = file_get_contents("my url here"); ?> var count = <?php echo $contents ?>; document.getelementbyid('div').innerhtml = count; sleep(1500); } } </script> </head> <body onload=update()> <iframe id="iframe" src="my url" style="visibility: hidden;"/> <table width=100% height=100%> <tr height=80%> <td width=100%> <center> <div id="div"></div> </center> </td> </tr> </table> </body> </html>
when viewing source code in-browser, var
set correctly, won't set content of div
. positive url php script pointing accurate , contains script grep number external page.
thanks in advance!
assuming value of file_get_contents("my url here")
yields 42
(for example), aware you're creating infinite loop set content of #div
42
on , on again every 1.5 seconds? i.e., var count
always, 42
, won't change, since php evaluated once on server.
furthermore suspect sleep
function pose bit of problem, since creates tight loop. wouldn't surprised if script blocked browser without ever updating since it's caught in infinite loop. way delay execution in javascript use window.settimeout
or window.setinterval
.
i guess you're looking ajax solution of kind, if tell want do. since you're including jquery (without using it), should have @ jquery.get()
, coupled setinterval
update div
periodically.
something like:
window.setinterval(function () { $.get("my url here", function (data) { $('#div').html(data); }); }, 1500);
Comments
Post a Comment