我正在使用安卓的J2V8端口(https://github.com/eclipsesource/J2V8)。
是否可以启用上下文方法(setInterval,setTimeout,.)?
V8 runtime = V8.createV8Runtime("global");
runtime.executeIntegerScript("setInterval(function() {
console.log(\"Hello\"); }, 1000)");它失败时出现了错误:"ReferenceError: setInterval未定义“。
或者引擎只能执行纯javascript?
发布于 2018-11-02 10:08:48
V8引擎只能执行纯javascript。但是,通过在引擎中注册setTimeout方法,当您得到对该函数的调用时,您也可以进行同样的模拟。就像下面这样。但是您必须使用Executors.newSingleThreadScheduledExecutor()
private var setTimeOutCallback: JavaCallback = JavaCallback { _, v8Array ->
val v8Function = v8Array.getObject(0) as V8Function
val time = v8Array.getInteger(1).toLong()
val taskId = Random.nextInt(1000, 9999)
val task = v8Executor.schedule({
v8Function.call(runtime, null)
}, time, TimeUnit.MILLISECONDS)
taskId
}https://stackoverflow.com/questions/52181896
复制相似问题