用户连续点击同一个按钮,如何判断用户间断了3秒以上没有点击?
# coffescript
debounce: (fn, context, wait)->
tid = null
# if content is a number, assign it to wait
if not isNaN context
wait = context if context > 0
context = undefined
wait = 200 if isNaN(wait) or wait < 0
->
args = arguments
clearTimeout tid
tid = setTimeout ->
fn?.apply context, args
return
, wait
return
以上为前端语法,参考原理
// 点击发送按钮
(void)giftSendViewPressedSendBtn:(DNGiftSendView *)giftSendView {
// 礼物单元格数据模型
NSIndexPath *currentIndexPath = giftSendView.viewModel.currentIndexPath;
NSArray<DNGiftSendViewCellViewModel > cellmodels = [giftSendView.viewModel.cellViewModels objectAtIndex:currentIndexPath.section];
DNGiftSendViewCellViewModel *cellmodel = [cellmodels objectAtIndex:currentIndexPath.row];
// 提示
if (!currentIndexPath) {
[DNToastView showToastWith:@"请选择礼物"];
return;
}
// 发送礼物时间间隔定时器 init
[self giftSendViewScheduledTimerInitWithDuration:kGiftSendViewScheduledTimerInitWithDuration andGift:cellmodel.gift];
// 发送礼物
[[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(giftSendViewSendGifts) object:nil];
[self performSelector:@selector(giftSendViewSendGifts) withObject:nil afterDelay:kGiftSendViewSendGiftDelayDuration];
}
// 发送礼物时间间隔定时器 init
(void)giftSendViewScheduledTimerInitWithDuration:(NSTimeInterval)interval andGift:(DNGift *)gift {
if (!_giftSendViewScheduledTimer) {
__weak typeof (self) weakSelf = self;
_giftSendViewScheduledTimer = [NSTimer scheduledTimerWithTimeInterval:interval firing:^{
if (weakSelf.giftSendViewScheduledTimer.isValid) {
// 发送本地消息
if([weakSelf giftSendViewSendLocalMsg:gift]) {
[weakSelf.waittingSendGifts addObject:gift];// 增加等待发送礼物
}
[weakSelf giftSendViewScheduledTimerDestroy];// 发送礼物时间间隔定时器 destroy
}
}];
}
}
// 发送多个礼物
(void)giftSendViewSendGifts {
// 礼物数为0
if (self.waittingSendGifts.count == 0) {
return;
}
// 发送多个礼物
__weak typeof(self) weakSelf = self;
[_artistLiveDataController requestGiftsSendWithActivityId:self.activityId andGifts:self.waittingSendGifts andCallback:^(NSError error, NSArray<DNGift > *sendFailGifts) {
if (error) {
// 余额不足提示
if (error.code == DNDataControlErrorUserRemainingMoneyNotEnough) {
[weakSelf giftSendFailedViewAnimationShow];// 显示余额不足提示
[self requestUserRemainingMoney];// 请求用户余额
}
// 其他错误处理
if (error.code == DNDataControlErrorGiftSendFailed) {
}
// 把扣掉的余额加回来
for (int i=0; i<sendFailGifts.count; i++) {
DNGift *failureGift = [sendFailGifts objectAtIndex:i];
weakSelf.giftSendView.remainMoney += failureGift.giftPrice.floatValue;// 减少余额
NSLog(@"--------发送礼物[%@]失败!!!--------",failureGift.giftName);
}
} else {
NSLog(@"--------发送礼物成功!!!--------");
}
}];
// 清空等待发送的礼物
weakSelf.waittingSendGifts = [NSMutableArray arrayWithCapacity:0];
}
利用Objective-C的methodSwizzing,替换sendAction:to:forEvent:
方法,在替换的方法里面定义一个变量用来存储点击的时间。
可以参考:
http://www.jianshu.com/p/e4f1...
4 回答4.2k 阅读
2 回答1.8k 阅读✓ 已解决
1 回答1.3k 阅读✓ 已解决
2 回答1.4k 阅读
1 回答1.2k 阅读
1 回答1.1k 阅读
1 回答900 阅读
你可以在点击按钮的时候开启一个定时器,判断时间间隔