+ -
当前位置:首页 → 问答吧 → 为什么Jquery中的 replaceWith 只运行一次.

为什么Jquery中的 replaceWith 只运行一次.

时间:2011-07-04

来源:互联网

function loopfun()
{
$.post("getonlineflag.php",{ username: "<?php echo $username ?>"},function(text){$("#gamerlist").replaceWith(text);});

$("#messagegame").append("Hello"); 
setTimeout('loopfun()', 4000);
}

本来是想每4秒运行一次上面代码,但是replaceWith这句只能运行第一次.而后面的append这句就每次都执行了的.这是为什么??

虽然后来我把replaceWith换成了 1个empty 操作和1个append操作,完成了我所要的功能,但这个replaceWith 为什么只执行一次任然困扰我.

作者: wxvp   发布时间: 2011-07-04

是不是浏览器缓存造成的,给url加一个随机数试试,比如加个时间戳:
JScript code
function loopfun() {
  $.post("getonlineflag.php",{ username: "<?php echo $username; ?>",t:"<?php echo time(); ?>"},function(text){$("#gamerlist").replaceWith(text); });
  $("#messagegame").append("Hello");  
  setTimeout('loopfun()', 4000);
}

作者: T5500   发布时间: 2011-07-04