# 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
你可以在点击按钮的时候开启一个定时器,判断时间间隔
debounce(防抖)
debounce(防抖)和throttle(节流)
// 点击发送按钮
(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) {
}
// 发送礼物时间间隔定时器 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) {
}
// 发送多个礼物
(void)giftSendViewSendGifts {
// 礼物数为0
if (self.waittingSendGifts.count == 0) {
}
// 发送多个礼物
__weak typeof(self) weakSelf = self;
[_artistLiveDataController requestGiftsSendWithActivityId:self.activityId andGifts:self.waittingSendGifts andCallback:^(NSError error, NSArray<DNGift > *sendFailGifts) {
}
利用Objective-C的methodSwizzing,替换
sendAction:to:forEvent:方法,在替换的方法里面定义一个变量用来存储点击的时间。可以参考:
http://www.jianshu.com/p/e4f1...