我看到你终于选择了一个答案,但我想我会投入两分钱,因为我做了这个例子并做了很长的解释。我希望这对您的理解有所帮助。
第一个示例有一个计时器变量,因此每次发生变化时都可以重置 10 秒。这样,单击具有您的类名的元素也会重置计时器。
旁注:在以下示例中,我使用.on()。如果您使用的是 1.7 之前的 jQuery,您可以简单地将.on()替换为.live()。
var tmrShowHide; // this variable will act as out timer for keeping up with time changes
// if you don't want to reset the 10 seconds everytime there is a change,
// then please see exmple 2
function divShowHide() { // this will be the method used to change the colors and reset the timer
clearTimeout(tmrShowHide); // clears previous timer so 10 seconds is reset
// the .toggle method will simply toggle the visible state (display in css) of the element it's called upon
// so if we start with one hidden and one visible, then this is all that is needed to make the change!
// also note that i'm calling both elements at once, as they'll both undergo the "toggle" change
$('#pf-blue-area, #pf-orange-area').toggle();
tmrShowHide = setTimeout(divShowHide, 10000); // resets timer to 10 seconds
}
$(function() { // Same as $(document).ready(function() { // just shorter!
// the following establiches our click event on the 2 class types
$(document).on('click', '.dab, .pfs', divShowHide);
// this will begin the timer and give us a variable that can be cleared as needed
tmrShowHide = setTimeout(divShowHide, 10000);
})
所以没有评论它很简单:
var tmrShowHide;
function divShowHide() {
clearTimeout(tmrShowHide);
$('#pf-blue-area, #pf-orange-area').toggle();
tmrShowHide = setTimeout(divShowHide, 10000);
}
$(function() {
$(document).on('click', '.dab, .pfs', divShowHide);
tmrShowHide = setTimeout(divShowHide, 10000);
})
下一个示例要短得多,因为它不会重置计时器,因此,单击类元素进行更改不会阻止它每 10 秒更改一次。
function divShowHide(e) { // this will be the method used to change the colors
// the .toggle method will simply toggle the visible state (display in css) of the element it's called upon
// so if we start with one hidden and one visible, then this is all that is needed to make the change!
// also note that i'm calling both elements at once, as they'll both undergo the "toggle" change
$('#pf-blue-area, #pf-orange-area').toggle();
}
$(function() { // Same as $(document).ready(function() { // just shorter!
// the following establishes our click event on the 2 class types
$(document).on('click', '.dab, .pfs', divShowHide);
// this will begin the unstoppable 10 second timer
setInterval(divShowHide, 10000);
})
所以没有评论它很简单:
function divShowHide(e) { $('#pf-blue-area, #pf-orange-area').toggle(); }
$(function() {
$(document).on('click', '.dab, .pfs', divShowHide);
setInterval(divShowHide, 10000);
})