john ab
john ab

Reputation: 73

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>

Upvotes: 3

Views: 119

Answers (2)

g.d.d.c
g.d.d.c

Reputation: 47988

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').

Upvotes: 1

Vivin Paliath
Vivin Paliath

Reputation: 95518

$(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).

Upvotes: 4

Related Questions