-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathASTimeUtils.swift
More file actions
54 lines (43 loc) · 1.23 KB
/
Copy pathASTimeUtils.swift
File metadata and controls
54 lines (43 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
// ASTimeUtils.swift
//
//
// Created by AntScript on 15/8/26.
//
//
import Foundation
public class ASTimeUtils {
public typealias Task = (cancel : Bool) -> ()
static public func delay(time:NSTimeInterval, task:()->()) -> Task? {
func dispatch_later(block:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(time * Double(NSEC_PER_SEC))),
dispatch_get_main_queue(),
block)
}
var closure: dispatch_block_t? = task
var result: Task?
let delayedClosure: Task = {
cancel in
if let internalClosure = closure {
if (cancel == false) {
dispatch_async(dispatch_get_main_queue(), internalClosure);
}
}
closure = nil
result = nil
}
result = delayedClosure
dispatch_later {
if let delayedClosure = result {
delayedClosure(cancel: false)
}
}
return result;
}
static public func cancel(task:Task?) {
task?(cancel: true)
}
}