我有一个工作添加/删除数字,我刚刚创建的显示当前网页的数量附加到内容滑块,这不是滑块的一个功能(请看http://sneakyrascal.com/coupon2 )
当点击箭头时,滑块每秒改变每个内容,但是数字会立即改变,所以当用户快速点击它5次时,内容不会达到这个数字,例如,页面可能在第三页,但数字是5!
我试着使用.delay它它不起作用...有没有办法添加到脚本中,这样点击就不会对当前数字产生直接影响?例如,在箭头上单击多次将每秒生效一次,这应该会起到我认为的效果……
$(function(){
$(".jcarousel-next-horizontal").click(function(){
var currentValue = $(".pages").text();
var five = "5";
var newValue = parseInt(parseFloat(currentValue)) + 1;
$(".pages").text(newValue);
if (currentValue==five) {
$(".pages").text("5");
}
});
$(".jcarousel-prev-horizontal").click(function(){
var currentValue = $(".pages").text();
var newValue = parseInt(parseFloat(currentValue)) - 1;
var one = "1";
$(".pages").text(newValue);
if (currentValue==one) {
$(".pages").text("1");
}
});
});
发布于 2011-09-06 17:37:08
最简单的方法可能是存储最后一次点击的时间,当新的点击发生时,将当前时间与上一次点击的时间进行比较,如果距离上一次点击过去的时间少于一秒,则忽略该点击。
时间可以存储在全局变量中,也可以使用jQuery的data()
方法与实际对象相关联(可能更好)。
例如:
$(".jcarousel-prev-horizontal").click(function(){
var currentTime = Date.now();
var lastClickTime = $(this).data("lastClickTime");
if (lastClickTime && (currentTime - lastClickTime < 1000)) {
return(false); // ignore the click
}
$(this).data("lastClickTime", currentTime); // record time of last click
var currentValue = $(".pages").text();
var newValue = parseInt(parseFloat(currentValue)) - 1;
var one = "1";
$(".pages").text(newValue);
if (currentValue==one) {
$(".pages").text("1");
}
});
发布于 2011-09-06 17:35:59
您可以使用全局变量来指示事件的时间
https://stackoverflow.com/questions/7317769
复制相似问题