admin管理员组文章数量:1290978
Hi I am relatively new to javascript and jQuery and while trying to create a function the runs in intervals of 100 milliseconds I encountered a problem.I seem to get in the console of firebug and error witch says that clasing() is not defined.This is my code:
$(document).ready(function() {
var prev = $("img.selected").prev();
var curent = $("img.selected");
var next = $("img.selected").next().length ? $("img.selected").next() : $("img:first");
$("img").not(":first").css("display","none");
function clasing() {
curent.removeClass("selected");
next.addClass("selected");
}
setInterval("clasing()",100);
});
What am I doing wrong here?Thank you
Hi I am relatively new to javascript and jQuery and while trying to create a function the runs in intervals of 100 milliseconds I encountered a problem.I seem to get in the console of firebug and error witch says that clasing() is not defined.This is my code:
$(document).ready(function() {
var prev = $("img.selected").prev();
var curent = $("img.selected");
var next = $("img.selected").next().length ? $("img.selected").next() : $("img:first");
$("img").not(":first").css("display","none");
function clasing() {
curent.removeClass("selected");
next.addClass("selected");
}
setInterval("clasing()",100);
});
What am I doing wrong here?Thank you
Share Improve this question edited Mar 20, 2012 at 19:55 CAbbott 8,0984 gold badges32 silver badges38 bronze badges asked Mar 20, 2012 at 19:52 user1146440user1146440 3632 gold badges9 silver badges19 bronze badges 1- 1 possible duplicate: stackoverflow./questions/2162267/… – jbabey Commented Mar 20, 2012 at 20:00
3 Answers
Reset to default 5You have a scope problem. Your variables (prev
, curent
and next
) are accessible inside .ready
scope, such as your function clasing
. But when you add this function to be called in a interval, using setInterval
, this function should be in a Global scope (inside window
object). Then, you should declare this function like window.clasing = function(){ ... }
, but, doing this, the variables declared in .ready()
scope will not be accessible running the function outside this scope, so all your variables must be in a global scope too. This should solve your problem.
However, this isn't a good programming practice, you should declare your variables inside clasing
function, then they will be accessible only in function scope; And your function must be delcared outside .ready()
function, and then you declare the interval inside .ready()
function.
So, your code should be liek this:
function clasing(){
var prev = $("img.selected").prev();
var curent = $("img.selected");
var next = $("img.selected").next().length ? $("img.selected").next() : $("img:first");
curent.removeClass("selected");
next.addClass("selected");
}
$(document).ready(function() {
$("img").not(":first").css("display","none");
setInterval("clasing()",100); //or just setInterval(clasing,100);
});
Change setInterval("clasing()",100);
to setInterval(clasing,100);
Change
setInterval("clasing()",100);
To
setInterval(function() {
clasing();
}, 100);
Right now your call to setInterval is running in global scope, but your function is defined inside your jquery function. Creating a closure will give you access to the jquery functions members.
本文标签: javascriptjQuery setInterval() undefined function errorStack Overflow
版权声明:本文标题:javascript - jQuery setInterval() undefined function error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741506126a2382331.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论