zoukankan      html  css  js  c++  java
  • swif debounce实现

    //关联`debounceAsyncAfter` 的调用的键。
    struct DebounceKey: Equatable, Hashable { fileprivate let lockedState = UnfairLockedState<UInt64>(0) init() {} static func == (lhs: DebounceKey, rhs: DebounceKey) -> Bool { return lhs.lockedState === rhs.lockedState } func hash(into hasher: inout Hasher) { hasher.combine(ObjectIdentifier(lockedState)) } }
    //限定时间内只发生一个去抖动的异步块 func debounceAsyncAfter(deadline: DispatchTime, key: DebounceKey, qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], execute work: @escaping () -> Void) { let epoch = key.lockedState.sync { (x) -> UInt64 in x += 1 return x } asyncAfter(deadline: deadline, qos: qos, flags: flags) { guard epoch == key.lockedState.sync({ $0 }) else { return } work() } }
    //限定时间内只发生去抖动的一组异步块 func debounceAsyncAfter(wallDeadline: DispatchWallTime, key: DebounceKey, qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], execute work: @escaping () -> Void) { let epoch = key.lockedState.sync { (x) -> UInt64 in x += 1 return x } asyncAfter(wallDeadline: wallDeadline, qos: qos, flags: flags) { guard epoch == key.lockedState.sync({ $0 }) else { return } work() } }
  • 相关阅读:
    开发趋势
    常用的meta
    meta基础
    HTTP请求方法GET和POST
    same-origin policy----wikipedia
    跨域——同源策略(译)
    DNS问答
    TCP/IP的整理
    鉴权方法
    Web攻击技术---OWASP top
  • 原文地址:https://www.cnblogs.com/wyxy2005/p/15762074.html
Copyright © 2011-2022 走看看