功能未定义? [英] Function is undefined?
问题描述
我正在努力找出为什么setTimeout
函数在尝试运行时找不到我的单选按钮标记功能的原因.
I'm struggling to find out why the setTimeout
function cannot find my radio button tagging function when trying to run.
这是我的代码...
var radioButtonTagging = (function(){
console.log('run');
$("span.radio").each(function(i, ele){
$(ele).addClass('radio'+i);
});
$(".radio1").click(function(){
console.log('fired');
$('#expandQuestion1').css('display','block');
});
});
if($('span.radio').length){
console.log('run');
radioButtonTagging();
} else {
console.log('trying to run timer');
setTimeout("radioButtonTagging()",2000);
}
我基本上只是在寻找带有收音机的班级,然后再添加带有收音机和索引的班级.
I'm basically just looking for spans with a class radio and adding a further class with radio plus the index.
我使用setInterval的原因是因为当它第一次尝试触发跨度时,因为它们是通过jquery插入的,所以它们不在适当的位置.所以在doc.ready期间没有完成.
The reason why I'm using the setInterval is because when it tries to fire the first time the span's are not in place as they are being inserted via jquery.. so are not finished during doc.ready..
任何帮助都会很棒
推荐答案
您正在将字符串传递给setInterval
,因此可以在其他范围内使用它.由于您要查找的功能是局部作用域的,因此找不到它.
You are passing a string to setInterval
, so it is evaled in a different scope. Since the function you are looking for is scoped locally, it can't find it.
不要将字符串传递给setInterval
,请传递函数.
Don't pass strings to setInterval
, pass functions.
这篇关于功能未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!