在 Swift 中取消计时事件?

我想在10秒钟内运行一段代码,但是我希望能够取消它,这样如果在这10秒钟之前发生了什么事情,代码就不会在10秒钟之后运行。

我一直在用这个,但是不能取消:

static func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure
)
}

我该怎么做呢?

25791 次浏览

This should work:

var doIt = true
var timer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: Selector("doSomething"), userInfo: nil, repeats: false)


//you have now 10 seconds to change the doIt variable to false, to not run THE CODE


func doSomething()
{
if(doIt)
{
//THE CODE
}
timer.invalidate()
}

Try this (Swift 2.x, see David's answer below for Swift 3):

typealias dispatch_cancelable_closure = (cancel : Bool) -> ()


func delay(time:NSTimeInterval, closure:()->()) ->  dispatch_cancelable_closure? {


func dispatch_later(clsr:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(time * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), clsr)
}


var closure:dispatch_block_t? = closure
var cancelableClosure:dispatch_cancelable_closure?


let delayedClosure:dispatch_cancelable_closure = { cancel in
if let clsr = closure {
if (cancel == false) {
dispatch_async(dispatch_get_main_queue(), clsr);
}
}
closure = nil
cancelableClosure = nil
}


cancelableClosure = delayedClosure


dispatch_later {
if let delayedClosure = cancelableClosure {
delayedClosure(cancel: false)
}
}


return cancelableClosure;
}


func cancel_delay(closure:dispatch_cancelable_closure?) {
if closure != nil {
closure!(cancel: true)
}
}


// usage
let retVal = delay(2.0) {
println("Later")
}
delay(1.0) {
cancel_delay(retVal)
}

From Waam's comment here: dispatch_after - GCD in swift?

I use @sas 's method in some projects, somehow this doesn't work anymore, maybe something changed after Swift 2.1.1. value copy instead of pointer?

the easiest work around method for me is:

var canceled = false
delay(0.25) {
if !canceled {
doSomething()
}
}

Swift 3 has DispatchWorkItem:

let task = DispatchWorkItem { print("do something") }


// execute task in 2 seconds
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2, execute: task)


// optional: cancel task
task.cancel()

Update for Swift 3.0

Set Perform Selector

perform(#selector(foo), with: nil, afterDelay: 2)

foo method will call after 2 seconds

 func foo()
{
//do something
}

To cancel pending method call

 NSObject.cancelPreviousPerformRequests(withTarget: self)

For some reason, NSObject.cancelPreviousPerformRequests(withTarget: self) was not working for me. A work around I thought of was coming up with the max amount of loops I'd allow and then using that Int to control if the function even got called.

I then am able to set the currentLoop value from anywhere else in my code and it stops the loop.

//loopMax = 200
var currentLoop = 0


func loop() {
if currentLoop == 200 {
//do nothing.
} else {
//perform loop.


//keep track of current loop count.
self.currentLoop = self.currentLoop + 1


let deadline = DispatchTime.now() + .seconds(1)
DispatchQueue.main.asyncAfter(deadline: deadline) {


//enter custom loop parameters
print("i looped")


self.loop()
}


}

and then elsewhere in your code you can then

func stopLooping() {


currentLoop = 199
//setting it to 199 allows for one last loop to happen. You can adjust based on the amount of loops you want to be able to do before it just stops. For instance you can set currentLoop to 195 and then implement a fade animation while loop is still happening a bit.
}

It's really quite dynamic actually. For instance you can see if currentLoop == 123456789, and it will run infinitely (pretty much) until you set it to that value somewhere else in your code. Or you can set it to a String() or Bool() even, if your needs are not time based like mine were.

You need to do this:

class WorkItem {


private var pendingRequestWorkItem: DispatchWorkItem?


func perform(after: TimeInterval, _ block: @escaping VoidBlock) {
// Cancel the current pending item
pendingRequestWorkItem?.cancel()
    

// Wrap the request in a work item
let requestWorkItem = DispatchWorkItem(block: block)
    

pendingRequestWorkItem = requestWorkItem


DispatchQueue.main.asyncAfter(deadline: .now() + after, execute:
requestWorkItem)
}
}


// Use


lazy var workItem = WorkItem()


private func onMapIdle() {


workItem.perform(after: 1.0) {


self.handlePOIListingSearch()
}
}

References

Link swiftbysundell

Link git