Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 3 files). Show all
Unified · Split
| @@ -312,22 +312,35 @@ void ScriptEngine::timerFired() { · 311 unmodified lines | |||
| 312 | } | 312 | } |
| 313 | } | 313 | } |
| 314 | 314 | ||
| 315 | void ScriptEngine::setupTimerWithInterval(const QScriptValue& function, int intervalMS, bool isSingleShot) { | 315 | QObject* ScriptEngine::setupTimerWithInterval(const QScriptValue& function, int intervalMS, bool isSingleShot) { |
| 316 | // create the timer, add it to the map, and start it | 316 | // create the timer, add it to the map, and start it |
| 317 | QTimer* newTimer = new QTimer(this); | 317 | QTimer* newTimer = new QTimer(this); |
| 318 | newTimer->setSingleShot(isSingleShot); | ||
| 319 | |||
| 318 | connect(newTimer, &QTimer::timeout, this, &ScriptEngine::timerFired); | 320 | connect(newTimer, &QTimer::timeout, this, &ScriptEngine::timerFired); |
| 319 | _timerFunctionMap.insert(newTimer, function); | ||
| 320 | 321 | ||
| 321 | newTimer->setSingleShot(isSingleShot); | 322 | // make sure the timer stops when the script does |
| 323 | connect(this, &ScriptEngine::scriptEnding, newTimer, &QTimer::stop); | ||
| 324 | |||
| 325 | _timerFunctionMap.insert(newTimer, function); | ||
| 322 | 326 | ||
| 323 | newTimer->start(intervalMS); | 327 | newTimer->start(intervalMS); |
| 328 | return newTimer; | ||
| 329 | } | ||
| 330 | |||
| 331 | QObject* ScriptEngine::setInterval(const QScriptValue& function, int intervalMS) { | ||
| 332 | return setupTimerWithInterval(function, intervalMS, false); | ||
| 324 | } | 333 | } |
| 325 | 334 | ||
| 326 | void ScriptEngine::setInterval(const QScriptValue& function, int intervalMS) { | 335 | QObject* ScriptEngine::setTimeout(const QScriptValue& function, int timeoutMS) { |
| 327 | setupTimerWithInterval(function, intervalMS, false); | 336 | return setupTimerWithInterval(function, timeoutMS, true); |
| 328 | } | 337 | } |
| 329 | 338 | ||
| 330 | void ScriptEngine::setTimeout(const QScriptValue& function, int timeoutMS) { | 339 | void ScriptEngine::stopTimer(QTimer *timer) { |
| 331 | setupTimerWithInterval(function, timeoutMS, true); | 340 | if (_timerFunctionMap.contains(timer)) { |
| 341 | timer->stop(); | ||
| 342 | _timerFunctionMap.remove(timer); | ||
| 343 | delete timer; | ||
| 344 | } | ||
| 332 | } | 345 | } |
| 333 | 346 | ||