TimeConstrained
TimeConstrained[expr, t] evaluates expr, stopping after t seconds. TimeConstrained[expr, t, failexpr] returns failexpr if the time constraint is not met.
Examples
Limit computation time:
TimeConstrained[Pause[5], 2]
(* $Aborted *)Provide a fallback value on timeout:
TimeConstrained[Pause[5], 1, "Timed out"]
(* "Timed out" *)Successful computation within time limit:
TimeConstrained[2 + 2, 1]
(* 4 *)Please visit the official Wolfram Language Reference for more details.