JQuery / AJAX call with timer

How can I implement the below code with a jQuery / AJAX timer (I created 2 classes of URL using web.py.Here 1st URL will return a random number from 1 to 250. I created an AJAX call in 2nd URL, so that when you click the button, AJAX will call the values ​​from the 1st URL and it will appear inside the text box). Now I just want to change the code, for example, every 5 seconds. AJAX should call numbers from the 1st URL and it should appear inside the text box. Is there any way to solve this problem using jQuery / AJAX timer.

import web
import random

urls = (
  '/', 'main',
  '/random','fill')


app = web.application(urls, globals(), True)

class main:
    def GET(self):
       return  random.randint(1,250)


class fill:
    def GET(self):
        return'''
        <html>
        <head>
        <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function(){
        $("button").click(function(){
          $.ajax({url:"http://127.0.0.1:8080/", success:function(result){
             $("#text").val(result);
          }});
        });});
        </script>
        </head>
        <body>


        <form>
        <input type = "text" id = "text"/>
        </form>
        <h2>Hit the button to view random numbers</h2>
        <button>Click</button>
        </body>
        </html>'''



if __name__ == "__main__":
    app.run()
+5
source share
2 answers

ajax , , n . , ajax.

$("button").click(function refreshText(){
    $.ajax({
        url:"http://127.0.0.1:8080/",
        success:function(result){
            $("#text").val(result);
            setTimeout(refreshText, 5000);
        }
    });
});
+5

javascript 5 :

<script type="text/javascript">
displayNumbers() {
    $.ajax({url:"http://127.0.0.1:8080/", success:function(result){ $("#text").val(result); }});
}

$(document).ready(function() {
    setInterval('displayNumbers', 5000);
}
</script>

, .

0

All Articles