Android JS iOS节流防抖函数备份

开发过程中遇到节流防抖问题,这里做个备份,方便以后查阅。

Android:

RxView.clicks(bt)
// 两秒之内点击多次只响应一次点击事件
 .throttleFirst(2, TimeUnit.SECONDS)
 .subscribe(new Observer() {
     @Override
     public void onSubscribe(Disposable d) {

     }

     @Override
     public void onNext(Object value) {
         Log.d(TAG, "发送了网络请求");
     }

     @Override
     public void onError(Throwable e) {
         Log.d(TAG, "对Error事件作出响应" + e.toString());
         // 获取异常错误信息
     }

     @Override
     public void onComplete() {
         Log.d(TAG, "对Complete事件作出响应");
     }
 });
 
  

iOS:

import UIKit
import RxSwift
import RxCocoa

public extension Reactive where Base: UIButton {
    var safeTap: ControlEvent {
        return ControlEvent.init(events: tap.throttle(.milliseconds(1500), latest: false, scheduler: MainScheduler.instance))
    }
    ///避免连续点击(1.5 秒响应一次)
    func safeDrive(onNext: @escaping ((Base) -> Void)) -> Disposable {
        return self.safeTap
            .asControlEvent()
            .asDriver()
            .drive {
                onNext(self.base)
            } onCompleted: {
                
            } onDisposed: {
                
            }
    }
}

public extension Reactive where Base: UITextField {
    ///避免连续调用(1.5 秒响应一次)
    func safeDrive(_ dueTime: RxSwift.RxTimeInterval = .milliseconds(1500), onNext: @escaping ((String) -> Void)) -> Disposable {
        return text.orEmpty
            .asDriver()
            .distinctUntilChanged()
            .debounce(dueTime)
            .drive(onNext: { (query) in
                onNext(query)
            }) {
                
            } onDisposed: {
                
            }
    }
}

public extension Reactive where Base: UISearchBar{
    ///避免连续调用(1.5 秒响应一次)
    func safeDrive(_ dueTime: RxSwift.RxTimeInterval = .milliseconds(1500), onNext: @escaping ((String) -> Void)) -> Disposable {
        return text.orEmpty
            .asDriver()
            .distinctUntilChanged()
            .debounce(dueTime)
            .drive(onNext: { (query) in
                onNext(query)
            }) {
                
            } onDisposed: {
                
            }
    }
}

public extension UIButton{
    ///避免连续调用(1.5 秒响应一次)
    func rxDrive(onNext: @escaping ((UIButton) -> Void)) -> Disposable {
        return self.rx.safeDrive(onNext: onNext)
    }
}

public extension UITextField{
    ///避免连续调用(1.5 秒响应一次)
    func rxDrive(onNext: @escaping ((String) -> Void)) -> Disposable {
        return self.rx.safeDrive(onNext: onNext)
    }
}

public extension UISearchBar{
    ///避免连续调用(1.5 秒响应一次)
    func rxDrive(onNext: @escaping ((String) -> Void)) -> Disposable {
        return self.rx.safeDrive(onNext: onNext)
    }
}

JS:

function debounce(fn, time) {
    let id = null;
    return function(...args) {
        let context = this
        clearTimeout(id);
        id = setTimeout(() => {
            /* 箭头函数没有自己的 this和 arguments */
            fn.call(context, args);
        }, time);
    }
}

function throttle(fn, time) {
    let timeout = null;
    return function(...args) {
        let context = this
        if (!timeout) {
            timeout = setTimeout(() => {
                timeout = null
                fn.apply(context, args)
            }, time)
        }
    }
}

export let lodash={
    debounce:debounce,
    throttle:throttle
}

你可能感兴趣的:(android,javascript,ios)