I would like to create a delay function in javascript that takes a parameter of amount of time to delay, so that I could use it do introduce delay between execution of javascript lines in my QML application. It would perhaps look like this:
function delay(delayTime) {
// code to create delay
}
I need the body of the function delay()
. Note that setTimeout()
of javascript doesn't work in QML.
Marcus' answer does the job, but there is one big problem.
The problem is that the callback keeps connected to
triggered
signal even after triggered once. This means that if you use that delay function again, the timer will triggers all callbacks connected before again. So you should disconnect the callback after triggered.This is my enhanced version of the delay function:
As suggested in the comments to your question, the Timer component is a good solution to this.
The above would be how I'm currently using it, and here's how I might have implemented the example in your question.
(Which doesn't do anything; read on)
Though the exact way you are looking for it to be implemented suggests that you are looking for it to block until the next line of your program executes. But this isn't a very good way to go about it as it would also block everything else in your program as JavaScript only runs in a single thread of execution.
An alternative is to pass a callback.
Which would allow you to use it as such.
Hope it helps!
Edit: The above assumes you're working in a separate JavaScript file that you later import into your QML file. To do the equivalent in a QML file directly, you can do this.
I'm not convinced that this is the solution to your actual problem however; to delay an animation, you could use PauseAnimation.
you can use QtTest