Below is a simplified version of a bigger QML program where a waiting loop (while) in a function is used to measure the time of an event (collision).
Everything in the code looks fine to me, and we expect when the collision occurs, the while loop terminates by the function's return statement, but in reality it crashes, I don't know why!

Qt Code:
  1. import QtQuick 2.12
  2. import QtQuick.Window 2.12
  3.  
  4. Window {
  5. id: window
  6. visible: true
  7. width: 1000; height: 800
  8. color: "royalblue"
  9. property real colTime_1: new Date().getTime()
  10. property real colTime_2
  11. property real speed
  12.  
  13. Rectangle {
  14. id: ball
  15. x: window.x + 50; y: window.y + 50
  16. width: 18; height: 18
  17. color: "white"
  18. radius: width/2
  19. }
  20.  
  21. // Move the ball
  22. onFrameSwapped: {
  23. ball.x += 5
  24. ball.y += 3
  25. }
  26.  
  27. Component.onCompleted: {
  28. if (catchColTime()) console.log("Speed = ", speed)
  29. }
  30.  
  31. function catchColTime() {
  32. while(true)
  33. if (ball.x >= window.width) {
  34. colTime_2 = new Date().getTime()
  35. speed = window.width / (colTime_2 - colTime_1)
  36. return true
  37. }
  38. }
  39. }
To copy to clipboard, switch view to plain text mode