admin 管理员组文章数量: 1086019
I have a few buttons on my page and I want to switch focus on each on of them with a certain delay. How I can achieve that with jquery or pure javascript. This is the idea I have for iterating along all my buttons but I obviously end up with the focus on my last button.
$(document).ready(function() {
var allButtons = $(":button");
for (i=0;i<=allButtons.length;i++) {
$('.category_button')[i].focus()
}
});
I have a few buttons on my page and I want to switch focus on each on of them with a certain delay. How I can achieve that with jquery or pure javascript. This is the idea I have for iterating along all my buttons but I obviously end up with the focus on my last button.
$(document).ready(function() {
var allButtons = $(":button");
for (i=0;i<=allButtons.length;i++) {
$('.category_button')[i].focus()
}
});
Share
Improve this question
asked Mar 11, 2011 at 23:07
toplesstopless
8,23111 gold badges60 silver badges87 bronze badges
3 Answers
Reset to default 6You can do this by creating a closure within your for loop and passing the index to the setTimeout
delay:
var allButtons = $(":button");
for (i = 0; i < allButtons.length; i++) {
(function(index) {
setTimeout(function() {
allButtons[index].focus();
}, 1000*index);
}(i));
}
See example here.
You can use setTimeout to call a function after a delay. The function can set the focus on your next button.
So pseudocode --
setTimeout(2000, focusOn(0));
// somewhere else
function focusOn(i) {
$('.category_button')[i].focus();
if (i + 1 < numButtons)
{
setTimeout(2000, focusOn(i + 1);
}
}
var currentButtonIndex = 0;
function FocusButton()
{
// focus current button index here
// increment counter
// some condition when to stop
// call FocusButton again with delay
// window.setTimeout(FocusButton,1000);
}
本文标签: javascriptHow can I delay focus event in jqueryStack Overflow
版权声明:本文标题:javascript - How can I delay focus event in jquery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744027733a2520935.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论