//
//  MPPositive_Debouncer.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/5/21.
//

import UIKit

class MPPositive_Debouncer: NSObject {
    static let shared = MPPositive_Debouncer()
    //计时器
    private var timer: Timer?
    //计时值
    private var delay: TimeInterval
    
    private override init() {
        delay = 0.4
        super.init()
    }
    deinit {
        timer?.invalidate()
        timer = nil
    }
    func call(_ action:@escaping (() -> Void)) {
        // 取消之前的延迟调用
        timer?.invalidate()
        // 设置新的延迟调用
        timer = Timer.scheduledTimer(withTimeInterval: delay, repeats: false) { _ in
            action()
        }
    }
}
