目前的qml js版本中不直接支持定时器,js中原有的setTimeout() 和clearTimeout()是无法直接使用的。
想要在js中使用定时器,只能在main.qml中开启一个Time来间接使用。
使用方法如下
main.qml
import QtQuick 1.0
import "testTime.js" as testTime
Rectangle {
id: screen;
width: 200; height: 200
Timer {
id: timeOut
interval: 700;
onTriggered: { testTime.connectTimeOut(); }
}
}
testTime.js
function setTimeout()
{
timeOut.running = true;
}
function clearTimeout()
{
timeOut.running = false;
}
function connectTimeOut() {
log("connectTimeOut");
}
另外注意: 这种方法只能在主线程中使用,通过WorkerScript创建的线程内是无法识别timeOut这个ID的。