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:
THE PROBLEM -> trigger('click')
executes, but the event doesn't trigger when inside looped (intervaled) function
$(document.ready(function(){
var checkForConfirmation = function(){
clearInter开发者_JAVA技巧val(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.
In the html file there is an anchor link <a id="anchorLink" href="#hiddenDiv">Show</a>
.
If I click on Show
link fancybox shows, as expected.
$("#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.
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.
Any suggestions?
Try:
$(document).ready(function(){
// ...
});
instead of:
$(document.ready(function(){
// ...
});
精彩评论