Daily Coding Problem: Problem #671

/**
 * This problem was asked by Facebook.
Given a function f, and N return a debounced f of N milliseconds.
That is, as long as the debounced f continues to be invoked, f itself will not be called for N milliseconds.
 * */
class Problem_671 {
    /*
    * parameter 1: function f
    * parameter 2: N, milliseconds
    * */
    fun debounced(f: () -> Unit, N: Int) {
        var currentTime = 0L
        val startTime = System.currentTimeMillis()
        if (currentTime == 0L || (startTime - currentTime) >= N) {
            f()
            currentTime = System.currentTimeMillis()
        }
    }

    fun functionF() {
        println("call me")
    }
}

 

posted @ 2020-10-02 10:22  johnny_zhao  阅读(120)  评论(0编辑  收藏  举报