繁体   English   中英

重写这个 ajax 代码?

[英]Rewrite this ajax code?

我确实有一个 ajax 代码将在单击按钮时工作。 但我需要它应该每隔 10 秒自动加载 function。 我怎样才能重写下面的代码?

<html>
<head>
  <title>Ajax with jQuery Example</title>
  <script type="text/JavaScript" src="jquery.js"></script>
  <script type="text/JavaScript">
  $(document).ready(function(){
    $("#generate").click(function(){
      $("#quote p").load("script.php");
    });
  });
  </script>
<style type="text/css">
    #wrapper {
      width: 240px;
      height: 80px;
      margin: auto;
      padding: 10px;
      margin-top: 10px;
      border: 1px solid black;
      text-align: center;
    }
  </style>
</head>
<body>
  <div id="wrapper">
    <div id="quote"><p> </p></div>
    <input type="submit" id="generate" value="Generate!">
  </div>
</body>
</html>
$(document).ready(function(){
  setInterval(function(){
    $("#quote p").load("script.php");
  }, 10000);
});

这将每 10 秒调用一次$("#quote p").load("script.php") (或更准确地说,匿名 function 包含此调用)( setInterval的第二个参数以毫秒为单位指定)。

你应该能够做到这一点:

setInterval(function () {
  $("#quote p").load("script.php");
}, 10000);

它将每 10 秒将 script.php 加载到$('#quote p')中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM