# How would I stop a while loop after n amount of time?

**URL:** https://discourse.julialang.org/t/how-would-i-stop-a-while-loop-after-n-amount-of-time/84299
**Category:** New to Julia
**Tags:** question
**Created:** [July 16, 2022, 3:22am UTC](https://discourse.julialang.org/t/how-would-i-stop-a-while-loop-after-n-amount-of-time/84299 "2022-07-16T03:22:42Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [July 16, 2022, 3:26am UTC](https://discourse.julialang.org/t/how-would-i-stop-a-while-loop-after-n-amount-of-time/84299/2 "2022-07-16T03:26:23Z")

</div>

> <https://stackoverflow.com/questions/51264467/timeout-function-in-julia>

> [@Timed loop](https://discourse.julialang.org/t/timed-loop/48802):
>
> I’m running a loop asynchronously. I’d like to have each iteration take at most max\_ns nanoseconds. Here’s what i have so far: function wait4it(t₀, max\_ns) t₁ = time\_ns() t = t₁ - t₀ if t \< max\_ns s = 1e-9\*(max\_ns - t) sleep(s) end nothing end max\_ns = 10^9 @async while true t₀ = time\_ns() sleep(0.1) # and things wait4it(t₀, max\_ns) end But I’m wondering if there is anything similar/better out there?

> [@Break function on time limit](https://discourse.julialang.org/t/break-function-on-time-limit/7376):
>
> Is there a way to break a function after a time limit? I don’t have any access to the function. FYI (if it helps) I want to break a JuMP solve after some seconds without knowing which solver performs the task. (the solver is chosen by the user) The best would be to have try catch block where I can somehow specify the time limit in.

---

_[View the full topic](https://discourse.julialang.org/t/how-would-i-stop-a-while-loop-after-n-amount-of-time/84299)._
