You can use an Adlib function which checks every second if the script has run long enough, so it can terminate immediately or change the state of a boolean variable so the script can run its last loop to the end
Do
...
$Seconds = $endtime - Round($diff/1000)
If TimerDiff($time) > ($endtime * 1000) Then ;Why $endtime here ? shouldn't be $Seconds??
...
Until IsArray($Pix) ; This will exit when found color green, is it OK?