/    Sign up×
Community /Pin to ProfileBookmark

How to yield a while loop?

Hi, I was wondering how you can yield a while loop until, let’s say, a button is pressed. Example:

“`javascript
while (true) {
console.log(“While loop thing happened”)

waitUntil(button.onclick) // this isn’t an actual function, but something like that.
}
“`

Thanks

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@cootheadDec 12.2021 — Hi there Bost,

what, exactly, is happening that you require

to be stopped on the click of a button?

[i]coothead[/i]
Copy linkTweet thisAlerts:
@BostauthorDec 13.2021 — @coothead#1640474 It's not being stopped on the click of a button. It's waiting until the button is pressed to stop yielding
Copy linkTweet thisAlerts:
@cootheadDec 13.2021 —  ...to stop yielding what, exactly?
Copy linkTweet thisAlerts:
@SempervivumDec 13.2021 — A real endless loop in JS is not appropriate as it would freeze the browser. However you might implement it by use of setTimeout :

HTML:
``<i>
</i> &lt;button id = "stop-btn"&gt; Stop&lt;/ button&gt;<i>
</i>
`</CODE>
JS:
<CODE>
`<i>
</i> const intv = 100;
let cont = true;
function delayAction()
{
console.log('action');
if (cont)
{
setTimeout(delayAction, intv);
}
}
delayAction();
document.getElementById('stop-btn').addEventListener(
'click', event = &gt; {
cont = false;
});<i>
</i>
`</CODE>
However I'm not really sure if this is what you require. When I take a look at your previous thread something like this comes to my mind:<br/>
HTML:
<CODE>
`<i>
</i> &lt;button id = "next-btn"&gt; Next !&lt;/ button&gt;<i>
</i>
`</CODE>
JS:
<CODE>
` <i>
</i>const intv = 100;
const snapshot = [ 1, 2, 4, 8 ]; // just for testing
let helperArr = [];
snapshot.forEach(function(item) {
helperArr.push(item);
});
function *gen(arr)
{
for (let item of arr)
{
// return item and wait for next request:
yield item;
}
}
const g = gen(helperArr);
document.getElementById('next-btn').addEventListener('click', event = &gt;
{
// get next item:
const result = g.next();
// no more items left?
if (result.done)
{
console.log('no more values');
}
else
{
console.log('current value = ' + result.value);
}
});<i>
</i>
``

Does any of these meet your requirement ?
Copy linkTweet thisAlerts:
@madelinerussell221Dec 14.2021 — Hi :)

I think a better way to handle this would be the use of a timer. This would allow the form to draw its controls and handle all input, such as clicking the button. You can also adjust the timer interval (ms) to your needs.

Another way is to use multi-threading. I would strongly recommend condition checks over the try/catch checks if possible. Enable/Disable the timer using the button and call the loop when the timer ticks. [Here is](https://www.eleken.co/saas-web-design) an example I put in my project:

``</CODE>
Timer loopTimer = new Timer();

<i> </i><CODE>private void Form1_Load(object sender, EventArgs e)
<i> </i>{
<i> </i> loopTimer.Interval = 100;
<i> </i> loopTimer.Tick += loopTimer_Tick;
<i> </i> loopTimer.Enabled = true;
<i> </i>}

<i> </i>void loopTimer_Tick(object sender, EventArgs e)
<i> </i>{
<i> </i> //perform the loop here at the set interval
<i> </i>}

<i> </i>private void button1_Click(object sender, EventArgs e)
<i> </i>{
<i> </i> //pause/play the loop
<i> </i> loopTimer.Enabled = !loopTimer.Enabled;
<i> </i>}</CODE>
<CODE>
``
Copy linkTweet thisAlerts:
@SempervivumDec 14.2021 — @madelinerussell221#1640547
  • 1. **Links removed by Site Staff so it doesn't look like you're spamming us. Please don't post them again.**

  • 2. Your answer is offtopic as the TO is asking for a java**script** solution.
  • ×

    Success!

    Help @Bost spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.14,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...