关于在jQuery的$(document).ready(function(){})里面建立一个时钟的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
shuizhongyue
V2EX    问与答

关于在jQuery的$(document).ready(function(){})里面建立一个时钟的问题

 
  •   shuizhongyue 2012-07-24 18:42:29 +08:00 4813 次点击
    这是一个创建于 4857 天前的主题,其中的信息可能已经有所发展或是发生改变。
    想实现一个动态显示倒计时的效果
    $(document).ready(function(){
    var count = 120,timerCount;

    function timeCount(){
    $("#time-count").text(count);
    timerCount = setTimeout('timeCount()',1000);
    }

    timeCount();
    });
    可是这个效果总是没法实现,在显示120的一秒后控制台提示:timeCount() is not define
    表示不理解,这timeCount()函数怎么会找不到
    9 条回复    1970-01-01 08:00:00 +08:00
    shuizhongyue
        1
    shuizhongyue  
    OP
       2012-07-24 18:49:17 +08:00
    没人对这个问题有兴趣?是这个是很低级的问题还是怎么的
    whtsky
        2
    whtsky  
       2012-07-24 18:53:20 +08:00
    function timeCount(){
    $("#time-count").text(count);
    timerCount = setTimeout('timeCount()',1000);
    }

    $(document).ready(function(){
    var count = 120;

    timeCount();
    });
    shuizhongyue
        3
    shuizhongyue  
    OP
       2012-07-24 18:56:10 +08:00
    @whtsky 恩,我知道这样有用,可是我不明白为什么会因为把timeCount()函数的定义放在$(document).ready(function(){})里面之后,就找不到了
    endintro
        4
    endintro  
       2012-07-24 19:02:57 +08:00   1
    $(document).ready(function(){
    var count = 120,timerCount;

    function timeCount(){
    $("#time-count").text(count);
    timerCount = setTimeout(timeCount,1000);
    console.log('a');
    }

    timeCount();
    sd4399340
        5
    sd4399340  
       2012-07-24 19:07:54 +08:00
    @endintro 为什么用‘timeCount()’不行啊,必须用函数名当参数?
    wong2
        6
    wong2  
       2012-07-24 19:15:09 +08:00   1
    setTimeout第一个参数填字符串的话,会在全局作用域被执行,而你的timeCount定义在那个匿名函数里,全局下是得不到的
    shuizhongyue
        7
    shuizhongyue  
    OP
       2012-07-24 19:18:10 +08:00
    @endintro 刚刚试了,这样可以,只是为什么setTimeout('timeCount()',1000)不行呢,我看网上的代码大多是这样写的啊
    wong2
        8
    wong2  
       2012-07-24 19:23:38 +08:00
    @shuizhongyue

    String literals are evaluated in the global context, so local symbols in the context where setTimeout() was called will not be available when the string is evaluated as code.

    摘自 https://developer.mozilla.org/en/DOM/window.setTimeout

    另外还会有和eval一样的安全隐患,最好不要用。。”网上的代码大多是这样写的“。。。= =
    benzhe
        9
    benzhe  
       2012-07-24 19:25:35 +08:00   1
    建议 LZ 了解一下 Javascript 强制类型转换、闭包或作用域链。
    setTimeout('timeCount()',1000); 里的 'timeCount()' 被强制转换为 function,并在全局 window 中寻找这个 timeCount ,而 timeCount 只被定义在包内,当然会出错。
    @endintro 的方法则是引用包内的 timeCount,也没有转换类型,所以是正常的
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     925 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 21:24 PVG 05:24 LAX 13:24 JFK 16:24
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86