1
shuizhongyue OP 没人对这个问题有兴趣?是这个是很低级的问题还是怎么的
|
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(); }); |
3
shuizhongyue OP @whtsky 恩,我知道这样有用,可是我不明白为什么会因为把timeCount()函数的定义放在$(document).ready(function(){})里面之后,就找不到了
|
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(); |
6
wong2 2012-07-24 19:15:09 +08:00 1
setTimeout第一个参数填字符串的话,会在全局作用域被执行,而你的timeCount定义在那个匿名函数里,全局下是得不到的
|
7
shuizhongyue OP @endintro 刚刚试了,这样可以,只是为什么setTimeout('timeCount()',1000)不行呢,我看网上的代码大多是这样写的啊
|
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一样的安全隐患,最好不要用。。”网上的代码大多是这样写的“。。。= = |