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
Add a ment  | 

3 Answers 3

Reset to default 5

You 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