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.
我如何使用jQuery/AJAX计时器实现下面的代码(我使用web.py创建了两个URL类)。这里第一个URL将返回1到250之间的随机数。我已经创建了一个AJAX调用第二个URL,所以,当你按下按钮,AJAX调用的值从1日URL,它将显示在文本框),现在我只是想修改代码在每5秒钟AJAX应该叫数字从1日URL和它应该显示在文本框内。使用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()
2 个解决方案
#1
5
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.
当ajax调用从服务器返回时,创建一个计时器,在n毫秒后再次轮询服务器。无论ajax调用需要多长时间完成,这种方法都应该能够工作。
$("button").click(function refreshText(){
$.ajax({
url:"http://127.0.0.1:8080/",
success:function(result){
$("#text").val(result);
setTimeout(refreshText, 5000);
}
});
});
#2
0
You could create a function and set a javascript timer to call it every 5 seconds:
您可以创建一个函数并设置一个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>
I haven't checked the code, but you get the idea.
我还没检查代码,但你懂的。
#1
5
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.
当ajax调用从服务器返回时,创建一个计时器,在n毫秒后再次轮询服务器。无论ajax调用需要多长时间完成,这种方法都应该能够工作。
$("button").click(function refreshText(){
$.ajax({
url:"http://127.0.0.1:8080/",
success:function(result){
$("#text").val(result);
setTimeout(refreshText, 5000);
}
});
});
#2
0
You could create a function and set a javascript timer to call it every 5 seconds:
您可以创建一个函数并设置一个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>
I haven't checked the code, but you get the idea.
我还没检查代码,但你懂的。