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 | 316 | // create the timer, add it to the map, and start it |
| 317 | 317 | QTimer* newTimer = new QTimer(this); |
| 318 | + newTimer->setSingleShot(isSingleShot); | |
| 319 | + | |
| 318 | 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 | 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) { | |
| 327 | − setupTimerWithInterval(function, intervalMS, false); | |
| 335 | +QObject* ScriptEngine::setTimeout(const QScriptValue& function, int timeoutMS) { | |
| 336 | + return setupTimerWithInterval(function, timeoutMS, true); | |
| 328 | 337 | } |
| 329 | 338 | |
| 330 | −void ScriptEngine::setTimeout(const QScriptValue& function, int timeoutMS) { | |
| 331 | − setupTimerWithInterval(function, timeoutMS, true); | |
| 339 | +void ScriptEngine::stopTimer(QTimer *timer) { | |
| 340 | + if (_timerFunctionMap.contains(timer)) { | |
| 341 | + timer->stop(); | |
| 342 | + _timerFunctionMap.remove(timer); | |
| 343 | + delete timer; | |
| 344 | + } | |
| 332 | 345 | } |
| 333 | 346 | |