What is the equivalent of JavaScript's setTime

2019-02-12 14:39发布

问题:

Not much to add; what is the equivalent of JavaScript's setTimeout on qtScript?

回答1:

Here's how you can extend your script language, by providing a self-contained C++ method (no need for bookkeeping of timer ids or so). Just create the following slot called "setTimeout":

void ScriptGlobalObject::setTimeout(QScriptValue fn, int milliseconds)
{
  if (fn.isFunction())
  {
    QTimer *timer = new QTimer(0);
    qScriptConnect(timer, SIGNAL(timeout()), QScriptValue(), fn);
    connect(timer, SIGNAL(timeout()), timer, SLOT(deleteLater()));
    timer->setSingleShot(true);
    timer->start(milliseconds);
  } else
    context()->throwError(tr("Passed parameter '%1' is not a function.").arg(fn.toString()));
}

And introduce that slot as function to the global object of your script engine. This can be done in different ways, e.g. just creating a QScriptValue-function via the QScriptEngine instance, and setting an accordingly named property on the engine's existing global object. In my case however, the entire ScriptGlobalObject instance is set as new global object, like this:

mScriptGlobalObject = new ScriptGlobalObject(this);
engine->setGlobalObject(engine->newQObject(mScriptGlobalObject));

Note that if you want to use "context()" as shown in the setTimeout code above, your ScriptGlobalObject should derive also from QScriptable, like this:

class ScriptGlobalObject : public QObject, protected QScriptable

In the script you can now use setTimeout to have a method invoked at a later time (as long as the QScriptEngine instance from which it originates isn't deleted in the meantime):

setTimeout(function() {
  // do something in three seconds
}, 3000);


回答2:

You can expose the QTimer as an instantiable class to the script engine. You can then instantiate it via new QTimer().

This is documented in Making Applications Scriptable.

Below is a complete example. The timer fires a second after the script is evaluated, prints timeout on the console, and exits the application.

// https://github.com/KubaO/stackoverflown/tree/master/questions/script-timer-11236970
#include <QtScript>

template <typename T> void addType(QScriptEngine * engine) {
   auto constructor = engine->newFunction([](QScriptContext*, QScriptEngine* engine){
      return engine->newQObject(new T());
   });
   auto value = engine->newQMetaObject(&T::staticMetaObject, constructor);
   engine->globalObject().setProperty(T::staticMetaObject.className(), value);
}

int main(int argc, char ** argv) {
   QCoreApplication app{argc, argv};

   QScriptEngine engine;
   addType<QTimer>(&engine);
   engine.globalObject().setProperty("qApp", engine.newQObject(&app));

   auto script =
         "var timer = new QTimer(); \n"
         "timer.interval = 1000; \n"
         "timer.singleShot = true; \n"
         "var conn = timer.timeout.connect(function(){ \n"
         "  print(\"timeout\"); \n"
         "  qApp.quit(); \n"
         "}); \n"
         "timer.start();\n";

   engine.evaluate(script);
   return app.exec();
}


回答3:

setTimeout and setInterval are not defined in ECMAScript specification because they are not JavaScript features. These functions are part of browser environments. So, QTscript does not have them.

You can use QTimer to achive this functionality. Here is a quick code how to use it in QTScript.

var timer = new QTimer();
timer.interval = 100; // set the time in milliseconds
timer.singleShot = true; // in-case if setTimout and false in-case of setInterval 
timer.timeout.connect(this, function(){console("in setTimout")});
timer.start();

Watch out for any bugs, I just coded it here.



回答4:

Qt provides an example in the 'context2d' project a way to access to the setInterval/clearInterval setTimeout/clearTimeout functionalities from the script.

In the 'Environment' class of this project, the startTimer function of a QObject is called each time that the script invokes setInterval/setTimeout. Then the timer identifier is added in a QHash with a reference on the javascript callback. When the countdown of the timer (c++ part) is over, the timerEvent of the 'Environment' class is called and then the javascript callback called..

Note that all the timers are killed in the Environment::reset() in order to clean the context.

see: https://doc.qt.io/archives/qt-4.7/script-context2d.html