[英]How can I delay focus event in jquery?
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.
我如何用jquery或纯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()
}
});
You can do this by creating a closure within your for loop and passing the index to the setTimeout
delay: 为此,您可以在for循环中创建一个闭包,然后将索引传递给
setTimeout
延迟:
var allButtons = $(":button");
for (i = 0; i < allButtons.length; i++) {
(function(index) {
setTimeout(function() {
allButtons[index].focus();
}, 1000*index);
}(i));
}
You can use setTimeout to call a function after a delay. 您可以使用setTimeout在延迟后调用函数。 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.