admin管理员组文章数量:1023009
Am trying to invoke a function after the loading of the html page. Its working fine and I want the function to be invoked after every 3 seconds and hence inserted the setInterval() for it. But the function is not at all getting called. Is there any problem with the syntax of setInterval()..?
<html>
<head>
<script src=".11.0/jquery.min.js">
</script>
<script>
$(document).ready( setInterval(function(){f()},3000); );
function f()
{
xhttp=new XMLHttpRequest();
xhttp.open("GET","/anu/get.php",true);
xhttp.send();
xhttp.onreadystatechange = function(){
if(xhttp.readyState==4){
var Display = document.getElementById('new');
Display.innerHTML = xhttp.responseText;
}
}
}
</script>
</head>
<body>
<p id="new"> </p>
<body>
</html>
Am trying to invoke a function after the loading of the html page. Its working fine and I want the function to be invoked after every 3 seconds and hence inserted the setInterval() for it. But the function is not at all getting called. Is there any problem with the syntax of setInterval()..?
<html>
<head>
<script src="http://ajax.googleapis./ajax/libs/jquery/1.11.0/jquery.min.js">
</script>
<script>
$(document).ready( setInterval(function(){f()},3000); );
function f()
{
xhttp=new XMLHttpRequest();
xhttp.open("GET","/anu/get.php",true);
xhttp.send();
xhttp.onreadystatechange = function(){
if(xhttp.readyState==4){
var Display = document.getElementById('new');
Display.innerHTML = xhttp.responseText;
}
}
}
</script>
</head>
<body>
<p id="new"> </p>
<body>
</html>
Share
Improve this question
edited May 4, 2014 at 16:18
Domenic
113k42 gold badges226 silver badges273 bronze badges
asked May 4, 2014 at 16:14
TrooperTrooper
1554 silver badges15 bronze badges
1
-
2
setInterval(function(){f()},3000);
is a very long winded way of saying:setInterval(f,3000);
– Quentin Commented May 4, 2014 at 16:17
1 Answer
Reset to default 4It should be:
$(document).ready(function () {
setInterval(function(){f()},3000);
});
The problem here is that setInterval
returns a number, and passing a number to $(document).ready(...)
does nothing. Instead, you need to pass $(document).ready(...)
a function, such as a function that starts off the setInterval
call.
The other problem is that you included a semicolon after the setInterval
, but semicolons are only valid on statements (which you can think of as "things that stand on their own line"). You can only pass expressions to functions like setInterval
, and expressions don't have semicolons, so your extra semicolon before the closing parenthesis is an error, because the JS engine sees a semicolon-terminated statement where it expects there to be a no-semicolon expression.
Also, you can shorten this to just
$(document).ready(function () {
setInterval(f, 3000);
});
or even
$(function () {
setInterval(f, 3000);
});
Am trying to invoke a function after the loading of the html page. Its working fine and I want the function to be invoked after every 3 seconds and hence inserted the setInterval() for it. But the function is not at all getting called. Is there any problem with the syntax of setInterval()..?
<html>
<head>
<script src=".11.0/jquery.min.js">
</script>
<script>
$(document).ready( setInterval(function(){f()},3000); );
function f()
{
xhttp=new XMLHttpRequest();
xhttp.open("GET","/anu/get.php",true);
xhttp.send();
xhttp.onreadystatechange = function(){
if(xhttp.readyState==4){
var Display = document.getElementById('new');
Display.innerHTML = xhttp.responseText;
}
}
}
</script>
</head>
<body>
<p id="new"> </p>
<body>
</html>
Am trying to invoke a function after the loading of the html page. Its working fine and I want the function to be invoked after every 3 seconds and hence inserted the setInterval() for it. But the function is not at all getting called. Is there any problem with the syntax of setInterval()..?
<html>
<head>
<script src="http://ajax.googleapis./ajax/libs/jquery/1.11.0/jquery.min.js">
</script>
<script>
$(document).ready( setInterval(function(){f()},3000); );
function f()
{
xhttp=new XMLHttpRequest();
xhttp.open("GET","/anu/get.php",true);
xhttp.send();
xhttp.onreadystatechange = function(){
if(xhttp.readyState==4){
var Display = document.getElementById('new');
Display.innerHTML = xhttp.responseText;
}
}
}
</script>
</head>
<body>
<p id="new"> </p>
<body>
</html>
Share
Improve this question
edited May 4, 2014 at 16:18
Domenic
113k42 gold badges226 silver badges273 bronze badges
asked May 4, 2014 at 16:14
TrooperTrooper
1554 silver badges15 bronze badges
1
-
2
setInterval(function(){f()},3000);
is a very long winded way of saying:setInterval(f,3000);
– Quentin Commented May 4, 2014 at 16:17
1 Answer
Reset to default 4It should be:
$(document).ready(function () {
setInterval(function(){f()},3000);
});
The problem here is that setInterval
returns a number, and passing a number to $(document).ready(...)
does nothing. Instead, you need to pass $(document).ready(...)
a function, such as a function that starts off the setInterval
call.
The other problem is that you included a semicolon after the setInterval
, but semicolons are only valid on statements (which you can think of as "things that stand on their own line"). You can only pass expressions to functions like setInterval
, and expressions don't have semicolons, so your extra semicolon before the closing parenthesis is an error, because the JS engine sees a semicolon-terminated statement where it expects there to be a no-semicolon expression.
Also, you can shorten this to just
$(document).ready(function () {
setInterval(f, 3000);
});
or even
$(function () {
setInterval(f, 3000);
});
本文标签: javascriptUsing setInterval with (document)readyStack Overflow
版权声明:本文标题:javascript - Using setInterval with $(document).ready - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745564526a2156368.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论