快速队列和延迟过程

时间:2017-10-12 05:44:03

标签: node.js swift queue delay

IN swift 3.0

我需要在点击按钮时在节点服务器上发送一个字典,用户可以频繁点击或点击此按钮。

所以我需要在一个接一个的订单中将这个字典发送到队列中并有一些延迟。并且同时用户可以多次HIT按钮,但是QUEUE将逐个执行其工作,不依赖于按钮Click。 这是我的代码

 func sendProductInCart(Product: NSString) {


        delayWithSeconds(5) {

            let msgToSend: String =  String.init(format: "__JSON__START__%@__JSON__END__", Product)

            print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
            print(msgToSend)

            let data: Data? = msgToSend.data(using: String.Encoding.utf8)
            _ = data?.withUnsafeBytes { self.outputStream.write($0, maxLength: (data?.count)!)
            }
        }
        }

func delayWithSeconds(_ seconds: Double, completion: @escaping () -> ()) {
        DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
            completion()
        }
    }

但这不起作用。 5秒后所有产品发送到5秒范围内。我一次需要一个。 请分享您的宝贵知识。

输出5秒后(我有10次点击按钮)

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
__JSON__START__{"type": "4","message": {"categoryid":"1","productId":"2","productprice":"10.00","productimage":"http:\/\/183.182.84.77:8082\/Content\/ProductImages\/chips2.JPG","productname":"B-Chips Premimum appetizer 40g -40","chanelname":"88719922048109810977","quantity":"2","producttype":"0","categoryName":"Alimentacion"}, "sender_id":"8109810977", "reciever_id": "8871992204", "time": "10-12-2017 05:16:09","connection_type":"cart"}__JSON__END__
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
__JSON__START__{"type": "4","message": {"categoryid":"1","productId":"2","productprice":"10.00","productimage":"http:\/\/183.182.84.77:8082\/Content\/ProductImages\/chips2.JPG","productname":"B-Chips Premimum appetizer 40g -40","chanelname":"88719922048109810977","quantity":"3","producttype":"0","categoryName":"Alimentacion"}, "sender_id":"8109810977", "reciever_id": "8871992204", "time": "10-12-2017 05:16:09","connection_type":"cart"}__JSON__END__
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
__JSON__START__{"type": "4","message": {"categoryid":"1","productId":"2","productprice":"10.00","productimage":"http:\/\/183.182.84.77:8082\/Content\/ProductImages\/chips2.JPG","productname":"B-Chips Premimum appetizer 40g -40","chanelname":"88719922048109810977","quantity":"4","producttype":"0","categoryName":"Alimentacion"}, "sender_id":"8109810977", "reciever_id": "8871992204", "time": "10-12-2017 05:16:09","connection_type":"cart"}__JSON__END__
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
__JSON__START__{"type": "4","message": {"categoryid":"1","productId":"2","productprice":"10.00","productimage":"http:\/\/183.182.84.77:8082\/Content\/ProductImages\/chips2.JPG","productname":"B-Chips Premimum appetizer 40g -40","chanelname":"88719922048109810977","quantity":"5","producttype":"0","categoryName":"Alimentacion"}, "sender_id":"8109810977", "reciever_id": "8871992204", "time": "10-12-2017 05:16:09","connection_type":"cart"}__JSON__END__
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
__JSON__START__{"type": "4","message": {"categoryid":"1","productId":"2","productprice":"10.00","productimage":"http:\/\/183.182.84.77:8082\/Content\/ProductImages\/chips2.JPG","productname":"B-Chips Premimum appetizer 40g -40","chanelname":"88719922048109810977","quantity":"6","producttype":"0","categoryName":"Alimentacion"}, "sender_id":"8109810977", "reciever_id": "8871992204", "time": "10-12-2017 05:16:09","connection_type":"cart"}__JSON__END__
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
__JSON__START__{"type": "4","message": {"categoryid":"1","productId":"2","productprice":"10.00","productimage":"http:\/\/183.182.84.77:8082\/Content\/ProductImages\/chips2.JPG","productname":"B-Chips Premimum appetizer 40g -40","chanelname":"88719922048109810977","quantity":"7","producttype":"0","categoryName":"Alimentacion"}, "sender_id":"8109810977", "reciever_id": "8871992204", "time": "10-12-2017 05:16:09","connection_type":"cart"}__JSON__END__
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
__JSON__START__{"type": "4","message": {"categoryid":"1","productId":"2","productprice":"10.00","productimage":"http:\/\/183.182.84.77:8082\/Content\/ProductImages\/chips2.JPG","productname":"B-Chips Premimum appetizer 40g -40","chanelname":"88719922048109810977","quantity":"8","producttype":"0","categoryName":"Alimentacion"}, "sender_id":"8109810977", "reciever_id": "8871992204", "time": "10-12-2017 05:16:10","connection_type":"cart"}__JSON__END__
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
__JSON__START__{"type": "4","message": {"categoryid":"1","productId":"2","productprice":"10.00","productimage":"http:\/\/183.182.84.77:8082\/Content\/ProductImages\/chips2.JPG","productname":"B-Chips Premimum appetizer 40g -40","chanelname":"88719922048109810977","quantity":"9","producttype":"0","categoryName":"Alimentacion"}, "sender_id":"8109810977", "reciever_id": "8871992204", "time": "10-12-2017 05:16:10","connection_type":"cart"}__JSON__END__
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
__JSON__START__{"type": "4","message": {"categoryid":"1","productId":"2","productprice":"10.00","productimage":"http:\/\/183.182.84.77:8082\/Content\/ProductImages\/chips2.JPG","productname":"B-Chips Premimum appetizer 40g -40","chanelname":"88719922048109810977","quantity":"10","producttype":"0","categoryName":"Alimentacion"}, "sender_id":"8109810977", "reciever_id": "8871992204", "time": "10-12-2017 05:16:10","connection_type":"cart"}__JSON__END__
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
__JSON__START__{"type": "4","message": {"categoryid":"1","productId":"2","productprice":"10.00","productimage":"http:\/\/183.182.84.77:8082\/Content\/ProductImages\/chips2.JPG","productname":"B-Chips Premimum appetizer 40g -40","chanelname":"88719922048109810977","quantity":"11","producttype":"0","categoryName":"Alimentacion"}, "sender_id":"8109810977", "reciever_id": "8871992204", "time": "10-12-2017 05:16:10","connection_type":"cart"}__JSON__END__

0 个答案:

没有答案