开发者

Attemp at jquery refreshing a div and php

开发者 https://www.devze.com 2023-04-01 04:02 出处:网络
I\'m trying to refresh a div with jquery that loads the time which is ge开发者_如何学Pythonnerated with php.

I'm trying to refresh a div with jquery that loads the time which is ge开发者_如何学Pythonnerated with php.

I've read some of the articles on jquery that were already on here and tried using those to refresh my div, but I failed.

echo '<!DOCTYPE html>
<html lang="en" dir="ltr"> 
<head> 
<meta charset="utf-8" />
<title>' . $title . '</title>
<link type="text/css" rel="stylesheet" media="all" href="styles/style.css" /> 
<link type="text/css" rel="stylesheet" media="all" href="styles/layout.css" /> 
<link type="text/css" rel="stylesheet" media="all" href="styles/' . $bodycss . '" /> 

</head> 
<body>

<script type="text/javascript" src="jquery.js"></script>
 <script type="text/javascript" src="jquery.timers.js"></script>
<script type="text/javascript">

var auto_refresh = setInterval(
function update() {
  $.get("'.$_SERVER['REQUEST_URI'].'", function(data) {
    $("#time").html(data);
    window.setTimeout(update, 500);
  });
}
</script>



<div id="container">
 <header id="header" class="margin5"> 
    <div id="info">  <a href ="home.php"> <img src="images/logo.jpg" alt="Oppasweb" /> </a> </div> 
    <div id="time"><time datetime=' . getTimeForTag() . '>' . getTime() . '</time></div> 


         <div class="clear"></div> 

';
}

The javascript should refresh the div every .5 of a second, generating the new time, however it doesn't do that, the time stays static.


your setInterval function is wrong (never closed).

you should not need it for that.

function update() {
  $.get("'.$_SERVER['REQUEST_URI'].'", function(data) {
    $("#time").html(data);
    window.setTimeout(function() {update();}, 500);
  });
}

$(document).ready(function () {
  update();
});

moreover, with looping so often, you might want to id your call (with a i++), and append to the times to the div instead of replacing the full html. You might want to look for the difference between setTimeout and setInterval.

note: true, full html as string is bad practice. but for test purposes, it's not that important (imo)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号