javascript - x is not defined, setTimeout problem - Stack Overflow

With the following code I get a clock is not defined error, why?$(function(){ function clock() {var n

With the following code I get a clock is not defined error, why?

$(function(){   
    function clock() {
        var nd = new Date();
        var h, m, s;
        h = nd.getHours();
        m = nd.getMinutes();
        s = nd.getSeconds();
        if (h <= 9) h = "0" + h;
        if (m <= 9) m = "0" + m;
        if (s <= 9) s = "0" + s;
        $('#digital-clock .hour').text(h+':');
        $('#digital-clock .min').text(m+':');
        $('#digital-clock .sec').text(s);
    }
    setTimeout('clock()', 1000);
});

With the following code I get a clock is not defined error, why?

$(function(){   
    function clock() {
        var nd = new Date();
        var h, m, s;
        h = nd.getHours();
        m = nd.getMinutes();
        s = nd.getSeconds();
        if (h <= 9) h = "0" + h;
        if (m <= 9) m = "0" + m;
        if (s <= 9) s = "0" + s;
        $('#digital-clock .hour').text(h+':');
        $('#digital-clock .min').text(m+':');
        $('#digital-clock .sec').text(s);
    }
    setTimeout('clock()', 1000);
});
Share asked Sep 23, 2010 at 15:53 MarwellnMarwelln 29.4k21 gold badges96 silver badges119 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

Because when you pass a string to setTimeout, the code inside it will be executed in global scope at timeout time. Code in global scope doesn't have access to any of the local variables present at the time you call setTimeout.

Don't pass a string to setTimeout, it invariably sucks (it's basically a deferred eval, and we all hate eval eh?). Instead use a Function object:

setTimeout(clock, 1000);

you can use an inline function expression to create your function too, for example:

setTimeout(function() {
    var nd= new Date();
    ...
}, 1000);

Try this:

$(function(){   
    function clock() {
        var nd = new Date();
        var h, m, s;
        h = nd.getHours();
        m = nd.getMinutes();
        s = nd.getSeconds();
        if (h <= 9) h = "0" + h;
        if (m <= 9) m = "0" + m;
        if (s <= 9) s = "0" + s;
        $('#digital-clock .hour').text(h+':');
        $('#digital-clock .min').text(m+':');
        $('#digital-clock .sec').text(s);
    }
    setTimeout(clock, 1000);
});

What's wrong with this?

$(function(){
    setTimeout(function() {
        var nd = new Date();
        var h, m, s;
        h = nd.getHours();
        m = nd.getMinutes();
        s = nd.getSeconds();
        if (h <= 9) h = "0" + h;
        if (m <= 9) m = "0" + m;
        if (s <= 9) s = "0" + s;
        $('#digital-clock .hour').text(h+':');
        $('#digital-clock .min').text(m+':');
        $('#digital-clock .sec').text(s);
    }, 1000);
});

Unless there's some reason you need to reference the function by name later... ?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743687501a4490392.html

相关推荐

  • javascript - x is not defined, setTimeout problem - Stack Overflow

    With the following code I get a clock is not defined error, why?$(function(){ function clock() {var n

    12天前
    130

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信