在JavascriptCore Framework for ObjectiveC中使用setInterval,setTimeout [英] Using setInterval, setTimeout in JavascriptCore Framework for ObjectiveC
问题描述
我一直在尝试新的Objective C JavascriptCore框架。
I've been experimenting with the new Objective C JavascriptCore Framework.
这很疯狂,但它似乎没有 setTimeout
或 setInterval
。哪个...我不明白。
This is crazy, but it seems that it doesn't have setTimeout
or setInterval
. Which... I don't understand.
- 我对此是对的吗?
- Are有替代方案吗?
我可能会在Objective C中创建计时器,但我的大部分库都是用Javascript编写的,除此之外,似乎很奇怪没有 setInterval
或 setTimeout
!!
I could potentially create the timer in Objective C but most of my library is written in Javascript, and aside from that, it seems just weird not having setInterval
or setTimeout
!!
我尝试了一些替代方法:
I've tried a few alternative methods:
window.setInterval(function(){ /* dosomething */ }, 1000);
setInterval(function(){ /* dosomething */ }, 1000);
var interval;
interval = window.setInterval(function(){ /* dosomething */ }, 1000);
interval = setInterval(function(){ /* dosomething */ }, 1000);
我无法监控JSVirtualMachine中甚至发生的情况。我所知道的是,当有一个 setInterval
被调用时,我的代码停止工作。
I have no way to monitor what's even happening in the JSVirtualMachine either. All I know is my code stops working when there is a setInterval
called.
任何帮助超级赞赏!
推荐答案
解决旧问题的新实现。
setTimout
, setInterval
和 clearTimeout
在JavaScriptCore的上下文中不可用。
setTimout
, setInterval
and clearTimeout
are not available on the context of JavaScriptCore.
您需要自己实施。我已经创建了一个快速的3类来解决这个问题。通常,示例仅显示 setTimeout
函数,但没有使用 clearTimeout
的选项。如果您使用JS依赖项,那么您很可能需要 clearTimeout
和 setInterval
函数。
You need to implement it by yourself. I've created a swift 3 class to solve this problem. Usually, the examples show only the setTimeout
function without the option to use clearTimeout
. If you are using JS dependencies, there's a big chance that you are going to need the clearTimeout
and setInterval
functions as well.
import Foundation
import JavaScriptCore
let timerJSSharedInstance = TimerJS()
@objc protocol TimerJSExport : JSExport {
func setTimeout(_ callback : JSValue,_ ms : Double) -> String
func clearTimeout(_ identifier: String)
func setInterval(_ callback : JSValue,_ ms : Double) -> String
}
// Custom class must inherit from `NSObject`
@objc class TimerJS: NSObject, TimerJSExport {
var timers = [String: Timer]()
static func registerInto(jsContext: JSContext, forKeyedSubscript: String = "timerJS") {
jsContext.setObject(timerJSSharedInstance,
forKeyedSubscript: forKeyedSubscript as (NSCopying & NSObjectProtocol))
jsContext.evaluateScript(
"function setTimeout(callback, ms) {" +
" return timerJS.setTimeout(callback, ms)" +
"}" +
"function clearTimeout(indentifier) {" +
" timerJS.clearTimeout(indentifier)" +
"}" +
"function setInterval(callback, ms) {" +
" return timerJS.setInterval(callback, ms)" +
"}"
)
}
func clearTimeout(_ identifier: String) {
let timer = timers.removeValue(forKey: identifier)
timer?.invalidate()
}
func setInterval(_ callback: JSValue,_ ms: Double) -> String {
return createTimer(callback: callback, ms: ms, repeats: true)
}
func setTimeout(_ callback: JSValue, _ ms: Double) -> String {
return createTimer(callback: callback, ms: ms , repeats: false)
}
func createTimer(callback: JSValue, ms: Double, repeats : Bool) -> String {
let timeInterval = ms/1000.0
let uuid = NSUUID().uuidString
// make sure that we are queueing it all in the same executable queue...
// JS calls are getting lost if the queue is not specified... that's what we believe... ;)
DispatchQueue.main.async(execute: {
let timer = Timer.scheduledTimer(timeInterval: timeInterval,
target: self,
selector: #selector(self.callJsCallback),
userInfo: callback,
repeats: repeats)
self.timers[uuid] = timer
})
return uuid
}
func callJsCallback(_ timer: Timer) {
let callback = (timer.userInfo as! JSValue)
callback.call(withArguments: nil)
}
}
用法示例:
jsContext = JSContext()
TimerJS.registerInto(jsContext: jsContext)
我希望有所帮助。 :)
I hope that helps. :)
这篇关于在JavascriptCore Framework for ObjectiveC中使用setInterval,setTimeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!