简体   繁体   中英

Rewrite this ajax code?

I do have a ajax code will work on a click of button. but I need it should automatically load function in between every 10 seconds. how can I rewrite below code??

<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);
});

This will call $("#quote p").load("script.php") (or more accurately, the anonymous function that contains this call) every 10 seconds (the second argument to setInterval is specified in milliseconds).

You should be able to do this:

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

Which will load script.php every 10 seconds into $('#quote p') .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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