admin管理员组文章数量:1353235
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=".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()
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./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()
Share
Improve this question
edited May 25, 2012 at 4:11
Sreehari K M
asked Jul 27, 2011 at 6:19
Sreehari K MSreehari K M
5752 gold badges6 silver badges24 bronze badges
2 Answers
Reset to default 5When 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 plete.
$("button").click(function refreshText(){
$.ajax({
url:"http://127.0.0.1:8080/",
success:function(result){
$("#text").val(result);
setTimeout(refreshText, 5000);
}
});
});
You could create a function and set a javascript timer to call it every 5 seconds:
<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.
本文标签: javascriptjQueryAJAX call with a timerStack Overflow
版权声明:本文标题:javascript - jQueryAJAX call with a timer - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743928266a2563365.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论