函数未定义?
我正在努力找出为什么 setTimeout
函数在尝试运行时找不到我的单选按钮标记函数。
这是我的代码...
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);
}
我基本上只是在寻找跨度一个类收音机,并添加一个带有收音机和索引的进一步类。
我使用 setInterval 的原因是因为当它第一次尝试触发时,跨度没有就位,因为它们是通过 jquery 插入的。所以在 doc.ready 期间没有完成。
任何帮助都会很棒
I'm struggling to find out why the setTimeout
function cannot find my radio button tagging function when trying to run.
this is my code...
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.
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..
Any help would be great
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将一个字符串传递给
setInterval
,因此它会在不同的范围内进行评估。由于您正在查找的函数在本地范围内,因此无法找到它。不要将字符串传递给
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.Don't pass strings to
setInterval
, pass functions.试试这个
try this
尝试:
setTimeout(radioButtonTagging, 2000);
参见这里:
http://jsfiddle.net/qUAZf/
Try:
setTimeout(radioButtonTagging, 2000);
See here:
http://jsfiddle.net/qUAZf/