Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 553
john ab Avatar asked Jul 18 '26 03:07

john ab


1 Answers

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

like image 134
Vivin Paliath Avatar answered Jul 19 '26 17:07

Vivin Paliath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!