jQuery的/ AJAX调用一个计时器 [英] jQuery/AJAX call with a timer

查看:295
本文介绍了jQuery的/ AJAX调用一个计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何能够实现低于code使用jQuery / AJAX定时器,(我已经建立2网址类使用web.py.Here第一URL将返回1和250.I之间的随机数已经创建了一个AJAX在第二个URL调用,这样,当你按下按钮,AJAX会从第1个URL调用的值,它会显示在文本框内),现在我只是想修改code一样,每5秒钟内AJAX应当从第一URL呼叫号码,它应在文本box.Is有内部显示任何方式通过使用jQuery / AJAX定时器来解决这个问题。

 导入Web
进口随机

网址=(
  '/', '主要',
  /随机,补)


应用= web.application(网址,全局(),真)

类主:
    高清GET(个体经营):
       返回random.randint(1250)


类填充:
    高清GET(个体经营):
        返回'''
        < HTML>
        < HEAD>
        <脚本类型=文/ JavaScript的SRC =htt​​p://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js>< / SCRIPT>
        <脚本类型=文/ JavaScript的>
        $(文件)。就绪(函数(){
        $(按钮)。点击(函数(){
          $阿贾克斯(网址:{url:http://127.0.0.1:8080/,成功:函数(结果){
             $(#文本)VAL(结果)。
          }});
        });});
        < / SCRIPT>
        < /头>
        <身体GT;


        <形式GT;
        <输入类型=文本ID =文本/>
        < /形式GT;
        < H2>点击按钮查看随机数< / H>
        <按钮>点击< /按钮>
        < /身体GT;
        < / HTML>'''



如果__name__ ==__main__:
    app.run()
 

解决方案

从服务器的Ajax调用返回时,创建一个定时器,将再次轮询服务器 N 毫秒后来。这种方法应该工作,无论需要多长时间进行Ajax调用来完成。

  $(按钮)。点击(函数refreshText(){
    $阿贾克斯({
        网址:http://127.0.0.1:8080/
        成功:函数(结果){
            $(#文本)VAL(结果)。
            的setTimeout(refreshText,5000);
        }
    });
});
 

How can i implement the below code with jQuery/AJAX timer,(I have created 2 URL classes by using web.py.Here 1st URL will return a random number between 1 and 250.I have created an AJAX call in the 2nd URL,so that when you hit the button ,AJAX will call the values from the 1st URL and it will display inside the text box),Now i just want to modify the code like during every 5 seconds AJAX should call the numbers from 1st URL and it should display inside the text box.Is there any way to solve this problem by 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()

解决方案

When the ajax call returns from the server, create a timer that will poll the server again n milliseconds later. This approach should work regardless of how long it takes for the ajax call to complete.

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

这篇关于jQuery的/ AJAX调用一个计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆