# Why doesn't Button work with loop? [duplicate]

It seems that I cannot use a loop in Dynamic. Quite ridiculous??

Here is my minimal example:

Grid[{{Button["test", i = 0; While[i < 10, Pause[10]; i++]], InputField[Dynamic[i]]}}]


I expect the dynamic i will increase by one every ten seconds(10 seconds gap is set to prevent it goes too fast to be observed). But the truth is i just starts from 0 and stops when I click on the button.

Thanks for your attention! Any comments and suggestion will be very welcome.

Edit: Thanks to comments from Kuba, I am now able to let the button update the value of i in a loop, but it still fails to work if I put them in DynamicsModule after check some other related questions.

The minimal example is:

DynamicModule[{i = 0},  Grid[{{Button["test", i = 0;
While[i < 10, i++; Pause[1]; FinishDynamic[]],
Method -> "Queued"],
InputField[Dynamic[i, UpdateInterval -> .5]]}}]]


## marked as duplicate by Kuba♦Jul 27 '17 at 14:26

• This will work: Button["test", i = 0; While[i < 10, Pause[1]; i++; FinishDynamic[]], Method -> "Queued"]. Method is a must and FinishDynamic[] is a remedy for this bug: 100828 – Kuba Jul 27 '17 at 10:03