Reputation: 1971
I have here a slideshow: fiddle
Codes: CSS:
#slideshow {
margin: 50px auto;
position: relative;
width: 240px;
height: 240px;
padding: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
}
#slideshow > div {
position: absolute;
top: 10px;
left: 10px;
right: 10px;
bottom: 10px;
}
#slideshow img {
max-width:240px;
max-height:240px;
}
ul {
list-style:none;
margin:0px;
padding:0px;
}
ul li {
float:left;
border-radius:10px;
width:10px;
height:10px;
border:1px solid white;
background:grey;
}
ul li.active {
background:black;
}
HTML:
<div id="slideshow">
<div>
<img src="http://farm6.static.flickr.com/5224/5658667829_2bb7d42a9c_m.jpg" />
</div>
<div>
<img src="http://farm6.static.flickr.com/5230/5638093881_a791e4f819_m.jpg" />
</div>
<div>
<img src="http://gillespaquette.ca/images/stack-icon.png" />
</div>
</div>
<ul></ul>
Jquery:
$("#slideshow > div:gt(0)").hide();
var maxindex = $('#slideshow > div').length;
var index = 0
var interval = 3 * 1000; // 3 seconds
var timerJob = setInterval(traverseSlideShow, interval);
function traverseSlideShow() {
console.log("current index: " + index);
$('#slideshow > div')
.stop()
.fadeOut(1000);
$('#slideshow > div').eq(index)
.stop()
.fadeIn(1000);
$('ul li').removeClass('active');
$('ul li:eq(' + index + ')').addClass('active');
index = (index < maxindex - 1) ? index + 1 : 0;
}
for (var i = 0; i < maxindex; i++) {
$('ul').append('<li class="' + (i == 0 ? 'active' : '') + '"></li>');
}
$(document).on('click', 'ul li', function () {
index = $(this).index();
traverseSlideShow();
clearInterval(timerJob);
timerJob = setInterval(traverseSlideShow, interval);
});
As you can see in the slideshow, when you click one of the buttons () the slideshow goes to a photo that the button you clicked is related with.
What I'm trying to do is very simple, the function accept 1 request every 1 second at maximum so user can not spam-click on the buttons, that means the function should break 1 second with every click and prevent loading a different image for different button through this 1 second(in this 1 second the slideshow should load the photo requested).
I'm trying to make my question more clear, lets say we have the three buttons 1, 2, 3 click on button 1 the function takes 1 second to load the photo(through that 1 second if you click the button 2 or 3 the function doesn't load the photo for 2 or 3), and so on.
Upvotes: 0
Views: 972
Reputation: 15860
You can use that jQuery as:
$("#attrid").attr("disabled", true);
This will set an attribute to that button as disabled. Then you can use this JavaScript!
setInterVal(function(), timeInSeconds);
And from this again you can remove the disabled attribute from that!
Upvotes: 0
Reputation: 388396
Try a simple flag based workaround like
var timerFlag = false; // a flag to keep track of the click state
$(document).on('click', 'ul li', function () {
if(timerFlag){// if the click state is true within last 1 sec there was a click so don't do anything
return
}
index = $(this).index();
traverseSlideShow();
clearInterval(timerJob);
timerJob = setInterval(traverseSlideShow, interval);
setTimeout(function(){ //after a second set the flag to false
timerFlag = false;
}, 1000);
timerFlag = true;// set the click flag to true
});
Demo: Fiddle
Upvotes: 2