Swift 延迟运行代码

//
//  DelayRun.swift
//
//  Created by XuQing on 16/7/1.
//  Copyright ? 2016年 xuqing. All rights reserved.
//

import Foundation

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

func DelayRun(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;
}

func cancel(task:Task?) {
    task?(cancel: true)
}
时间: 2024-07-28 21:05:56

Swift 延迟运行代码的相关文章

Swift 延迟运行

// // DelayRun.swift // // // Created by XuQing on 16/7/1. // Copyright © 2016年 xuqing. All rights reserved. // import Foundation typealias Task = (cancel : Bool) -> () func DelayRun(time:NSTimeInterval, task:()->()) -> Task? { func dispatch_late

AppleWatch开发教程之Watch应用对象新增内容介绍以及编写运行代码

AppleWatch开发教程之Watch应用对象新增内容介绍以及编写运行代码 添加Watch应用对象时新增内容介绍 Watch应用对象添加到创建的项目中后,会包含两个部分:Watch App 和 WatchKit Extension,如图2.18所示.其中,Watch App部分位于用户的iWatch上,它目前为止只允许包含Storyboard文件和Resources文件.在我们的项目里,这一部分不包括任何代码.WatchKit Extension部分位于用户的iPhone安装的对应App上,这

bat批处理延迟运行脚本

@echo off:aaapause 这里是你需要运行的程序for /l %%i in (0,1,10000) do echo %%i>nulgoto aaa 当然bat延迟运行还有其他的一些方法 不过我这个算很简单了 你还可以使用两个for代替gotofor /l %%i in (0,1,10000) do echo %%i>nul 这里>nul忽略输出如果程序是无法自动回到cmd界面的那可能需要结束她的进程什么的 自然利用bat调用其他程序也能做到例如ntsd debug 等等 或者

博客园“可运行"代码

textarea <textarea style="width: 450; height: 300" id="theCode" title="双击运行代码" ondblclick="runCode('theCode');"> 这里放入运行的Html代码. </textarea><br /> <input onclick="runCode('theCode')" va

iOS开发之Swift调用Objective-C代码

本文写于2014.09.25 最近iOS开发之新编程语言Swift在iOS开发圈内反响比较大,国内外都有很多教程或小示例. 虽然Swift这门语言仍然在不断的进化之中,而且变动还是比较大,苹果公司也不承诺目前所写代码会在将来兼容,但仍挡不住iOS开发者的热情. 为什么要使用Swift调用Objective-C代码 目前Swift语言所编写的应用才刚刚可以使用Xcode 6 GM版本提交,而Objective-C作为苹果的主开发语言存在了很多年了.目前尚无成熟的Swift库可用,所以当前编写应用可

运行代码时报linker command failed with exit code 1 错误

一个c语言项目,在.h文件中原来只有一些方法的声明,后来我加入了一些变量声明后,编译的时候报错: 运行代码时报linker command failed with exit code 1 错误 怎么回事那? [解决方法] 将变量的声明都移到.c文件中去就好了.

JS-在线运行代码小工具

原理:window.open()方法,open一个新的空白页,然后把文本框中粘贴的代码通过DOM操作,写到新的代码页中, 再利用document.write的功能(写进去之前把其他的全部删掉,并且写进去的html代码是可以解析的.)完成想要的效果. window.open打开的新页面也是一个浏览器对象,也具有document.write这个方法:. 1 <!DOCTYPE html> 2 <html> 3 <!-- 4 作者:[email protected] 5 时间:20

服务程序后台运行代码

使程序脱离终端,完全放在后台运行代码: void daemon(void) {                                     int fr=0;                                                                                                             fr = fork();                                            

可运行代码 文本框 插入html code 更新以后出现乱码。怎么解决?

<!doctype html>transition <a id="timings-demo-btn" href="#" data-mce-href="#">click</a> <br /><div id="timings-demo"><div id="ease" class="demo-box">Ease</d