间隔函数内的jQuery .trigger('click')吗? [英] jQuery .trigger('click') inside interval function?
问题描述
这是来自此处的改写的问题.经过一些测试,我隔离了该问题,但没有解决该问题的线索.无需阅读上一个问题,这是简化的代码:
This is a rephrased question from here. After some testing I isolated the problem, but have no clue on fixing it. No need to read the previous question, this is the simplified code:
问题-> 执行trigger('click')
,但是在内部循环(间隔)函数中,事件不会触发
THE PROBLEM -> trigger('click')
executes, but the event doesn't trigger when inside looped (intervaled) function
$(document.ready(function(){
var checkForConfirmation = function(){
clearInterval(checkInterval);
$("#anchorLink").trigger('click');
}
var checkInterval = setInterval(function(){checkForConfirmation()}, 5000);
});
该函数按时间间隔被调用.回复正确的响应后,间隔会停止,并模拟点击锚点链接.
The function is being called in intervals. When the proper response is replied, interval stops, and simulates the click on an anchor link.
在html文件中,有一个锚链接<a id="anchorLink" href="#hiddenDiv">Show</a>
.
它指向具有某些内容的隐藏div.单击锚点链接时,我正在使用Fancybox插件显示隐藏的div.
In the html file there is an anchor link <a id="anchorLink" href="#hiddenDiv">Show</a>
.
It points to the hidden div which has some content. I'm using Fancybox plugin to show the hidden div when the anchor link is clicked.
如果我单击Show
链接,则按预期显示的花式框.
如果我从后端得到响应,则代码将按预期执行,但未显示fancybox.
如果将$("#anchorLink").trigger('click');
移到checkForConfirmation
函数之外,则会显示花哨框.
当我用$("#anchorLink").text('Im clicked');
替换$("#anchorLink").trigger('click');
时,字符串显示在<a id="ancoredLink">
标记中.
If I click on Show
link fancybox shows, as expected.
If I get the response from the back-end, code executes as expected, but fancybox is not shown.
If I move $("#anchorLink").trigger('click');
outside the checkForConfirmation
function, fancybox shows.
When I replace $("#anchorLink").trigger('click');
with $("#anchorLink").text('Im clicked');
the string shows in the <a id="ancoredLink">
tag.
这是摘要,我已经在不同情况下尝试过.
问题显然是在循环功能中触发 click 事件. $("#anchorLink")
选择器是可访问的,它可以从其他任何地方正确触发它.显然,在循环功能内触发鼠标事件是有问题的.
This is the summary, I have tried it in different situations.
The problem is obviously in triggering the click event while in looping function. The $("#anchorLink")
selector is accessible, it is triggering it correctly from everywhere else. Obviously there is a problem in triggering the mouse event inside looping function.
有什么建议吗?
推荐答案
尝试:
$(document).ready(function(){
// ...
});
代替:
$(document.ready(function(){
// ...
});
这篇关于间隔函数内的jQuery .trigger('click')吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!