# Stopping endless button with code

**URL:** <https://forum.netlogo.org/t/stopping-endless-button-with-code/880>\
**Category:** Miscellaneous\
**Created:** [October 4, 2026, 2:43pm UTC](https://forum.netlogo.org/t/stopping-endless-button-with-code/880 "2026-10-04T14:43:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anders\_ekeland](https://forum.netlogo.org/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@anders\_ekeland](https://forum.netlogo.org/u/anders_ekeland)\
**Post date:** [October 4, 2026, 2:43pm UTC](https://forum.netlogo.org/t/stopping-endless-button-with-code/880/1 "2026-10-04T14:43:18Z")

</div>

I have a endless go button - that works well - when I click it exceution stops, but I would like run it the same number of time (ticks) every time, a huge number to se how much time it takes to complete a given task - on average - within a certain time frame = number of ticks.

But to measure the time for each try (robots and tasks are spread randomly again - when the task is completed - number of ticks is writen to a file and ticks reset.

I defined a global variable - and incremented it by 1 for every tick - it is completely independet of ticks,

but in the globals monitor I can oberve that this global run variable is reset each time ticks are reset.

If I stop it manually I get the file I want:

| Tics start | No of ticks |
| --- | --- |
| 0 | 314 |
| 0 | 77 |
| 0 | 296 |
| 0 | 237 |
| 0 | 494 |
| 0 | 311 |
| 0 | 130 |
| 0 | 224 |

But it should not be dependent on ticks at all! It seems to me it either resetting of ticks or the it the writing of the number of ticks to a file that resets the “runs” avariable.

If I just let it run it crashes with this error message

 ![image](https://forum.netlogo.org/uploads/default/original/1X/ea51090eb211cef76def807db3793ddb96bdb551.png)

All hinst very welcome!

---

<div class="post-metadata">

**Author:** ![mrkramer\_wageningen](https://forum.netlogo.org/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@mrkramer\_wageningen](https://forum.netlogo.org/u/mrkramer_wageningen)\
**Post date:** [October 5, 2026, 7:49am UTC](https://forum.netlogo.org/t/stopping-endless-button-with-code/880/2 "2026-10-05T07:49:17Z")

</div>

Hi Anders,

As for the error message I have no clue.

For your counter being reset, my guess is that the code contains a call `clear-all`, which will wipe out (set to zero) all variables.  
If it is just one variable that you want to keep over runs, you could make a local copy (using `let`) just before `clear-all` and restore the value after `clear-all`.  
Another trick that I have been using is an input box in the interface to hold your counter. Even though it is named “Input”, you can use it as a true variable. And it retains its value at `clear-all`. [Only remember to reset it manually before your experiment…]

Did you think of using BehaviorSpace for your experiments?

Good luck,  
Mark

---

<div class="post-metadata">

**Author:** ![anders\_ekeland](https://forum.netlogo.org/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@anders\_ekeland](https://forum.netlogo.org/u/anders_ekeland)\
**Post date:** [October 5, 2026, 9:21am UTC](https://forum.netlogo.org/t/stopping-endless-button-with-code/880/3 "2026-10-05T09:21:46Z")

</div>

Thanks a lot Mark! You made my day!

Yes it was a clear-all call, I thought it only worked on objects created or breeded. I absolutely do not need to clear all everything - on the contrary - bad habit have clear all everywhere.

When the global variable (runs) is not cleared - it is trivial to just count then number of “runs” - then just call stop when you reach a certain number of “runs”.

I am a bit surprise that this easy way is not a more used example in the “literature” and you tube videos. I mean - while clicking the stop button is very useful when you develop the mode - if you need to try to replicate results - it is very important running the same number of ticks, amount of abstract “time”.

---

<div class="post-metadata">

**Author:** ![mrkramer\_wageningen](https://forum.netlogo.org/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@mrkramer\_wageningen](https://forum.netlogo.org/u/mrkramer_wageningen)\
**Post date:** [October 6, 2026, 9:05am UTC](https://forum.netlogo.org/t/stopping-endless-button-with-code/880/4 "2026-10-06T09:05:06Z")

</div>

Good to hear that it solved your problem.

However, `clear-all` is there for a good reason and also used in (almost) all example models.  
For further work with NetLogo, you will definitely need it.

Forever buttons are not really meant for replication. They can be combined with some stop condition (in the `go` procedure, for example).  
For replicability in the interface, the command line of the Command Center is a very easy tool. Just type `go` there and press Enter, then repeat it as often as you need. Better use `repeat … [go]` (fill in your number of steps).  
If you don’t want to type commands, you can add a button for `repeat … [go]`, with or without another display name.  
And then, of course, you want to control the number of repetitions by means of a slider or input box 😉
