javascript setInterval()变量作用域问题

来源:百度知道 编辑:UC知道 时间:2024/06/14 02:12:30
function updateHeight(){
var updateheight = setInterval('adjustHeight()', 1000);
}

function adjustHeight(){
if (objload) {
obj.adjustHeight();
clearInterval(updateheight);
}
}

问:updateheight是函数内的局部变量,clearInterval(updateheight)这句是否有效?是因为setInterval是独立于加载进程的原因吗?谢谢。
谢谢。全文太长,这部分代码的意思就是等一个obj加载完了再改大小。我也觉得是无效,但是拿不准,不想再增加全局变量,把它改成这样了:
function updateHeight(){
var updateheight = setInterval(function(){
if (objload) {
obj.adjustHeight();
clearInterval(updateheight);
}
}
, 1000);
}

text44:积分会送给辛巴达的,我又专门给你开了一个问题,麻烦你再回答一下吧

对了, updateheight 的作用域只限于函数内.

var updateheight ;
function updateHeight(){
updateheight = setInterval('adjustHeight()', 1000);
}
这样就可以了

提醒一下: 变量名和函数名太相似了, 千万别看错了

你最好把全文贴出来 这样看不出来 你2个函数引用时是怎么个情形

按道理来说 clearInterval(updateheight)是无效的 如果updateheight真的是局部的话,不过不是没特殊情况 要看代码才知道

updateheigth 只能在updateHeight() 这个Function里有用。
只能定义全局
var updateheight;
function updateHeight(){
updateheight = setInterval(function(){
if (objload) {
obj.adjustHeight();
clearInterval(updateheight);
}
}
, 1000);
}

回答完了才看到问题的最后一句话。=。=。
还是提交了。