How do I stop a while loop with a button - JavaScript - javascript

Hello guys I'm looking for a solution for this program:
var i = 0;
function timer(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds) {
break;
}
}
}
$w.onReady(function () {
while (i === 0) {
dosomething();
timer(1000);
}
});
export function button5_click(event) {
i = 1;
}
My question is how can I stop this while with that button function?

You can do this way!
var flag = true;
let i = 0;
function loopFunc() {
if (flag == true){
i++;
console.log( `Do something here ${i}` );
setTimeout(loopFunc, 100);
}
}
loopFunc();
function stop(){
flag = false;
}
<button onclick="stop();">Stop Loop</button>

You could have a boolean which is checked if true in the while loop and if it true, the while loop will return. Add in an event listener to make that boolean true on click for the button. Don't forget to make the boolean false at the beginning of your script.

Using your code, you could do this
var stopLoop = false; // Add this to your code
var i = 0;
function timer(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds) {
break;
}
}
}
$w.onReady(function () {
while (i === 0) {
if(stopLoop) break;
dosomething();
timer(1000);
}
});
export function button5_click(event) {
stopLoop = true; // add this to your code
i = 1; // You can comment this out
}

Related

javascript I have a sequence that runs continuously, but need it to stop when an image is clicked

I have very little to no knowledge when it comes to using JavaScript. I have 24 of the same image given an id from q1 - q24. my code allows for the 24 images to be changed to image2 one at a time, but I need for it to stop and display a text/alert when image2 is clicked.
<script>
{
let num = 1;
function sequence()
{
let back = 1;
while (back < 25)
{
if(back == 1)
{
document.getElementById("q24").src = "question.jpg";
}
else
{
document.getElementById("q" + (back-1)).src = "question.jpg";
}
back++
}
document.getElementById("q" + num).src = "question2.png";
num = num + 1;
if(num > 24){num = 1;}
}
setInterval(sequence, 500);
}
</script>
Save the interval timer to a variable. Then add a click listener to all the images that stops the timer if the current image is the one showing question2.jpg.
{
let num = 1;
for (let i = 1; i <= 24; i++) {
document.getElementById(`q${i}`).addEventListener("click", function() {
if (i == num) {
clearInterval(interval);
}
});
}
let interval = setInterval(sequence, 500);
function sequence() {
for (let i = 1; i <= 24; i++) {
if (i == num) {
document.getElementById(`q${i}`).src = "question2.jpg";
} else {
document.getElementById(`q${i}`).src = "question.jpg";
}
num = num + 1;
if (num > 24) {
num = 1;
}
}
}
}
While I don't fully understand your use case, you could create a click event listener on the document and check the target's src in it.
document.addEventListener('click', function(e) {
if (e.target.src === 'question2.png') {
alert('Clicked question 2');
}
});

How do I wait before executing the next line of code?

I need the script to stop and wait 3 seconds before executing the code that follows (console.log('2 select question')).
var i = 0,
count = 5;
function f() {
if (i < count) {
i++;
console.log('Continue 1')
setTimeout(f, 1000);
} if (i >= count) {
console.log('2 select question')
console.log('3 select answer')
i = 0;
}
}
f();
Hello I don't know if i undrestand you but try this :
var i = 0,
count = 5;
function f() {
if (i < count) {
i++;
console.log('Continue 1')
setTimeout(f, 1000);
}
else if (i >= count) {
console.log('2 select question')
console.log('3 select answer')
i = 0;
}
}
f();

How Can I Get The mousedown Condition In for loop?

I want to get the mousedown condition in for loop.
But I cannot get it even if I mousedown trigger element(ex:#mousedown_button).
How can I get mousedown condition after starting for loop?
$("#mousedown_button").mousedown(function() {
for_loop()
});
function for_loop() {
for (i = 50; i > 0; i--) {
// I cannot get the count_flag(always false even if I mousedown #mousedown_button)
count_flag = get_mousedown_condition();
if (count_flag == false) {
break;
}
console.log(i);
var start = new Date();
while ((new Date() - start) < 250);
}
}
function get_mousedown_condition() {
// get the count flag
$("#mousedown_button").mouseover(function() {
return false;
}).mouseleave(function() {
return false;
}).mousedown(function() {
return true
}).mouseup(function() {
return false;
});
return false;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id='mousedown_button'>Click</div>
the way you tried, you bind all the event-handlers like 50 times and their return value will never enter your desired scope, because i am not sure what you try - i just leave this as an answer how you could store your "count_flag" and retrieve it in your loop
var count_flag = false;
function for_loop() {
for (i = 50; i > 0; i--) {
if (count_flag == false) {
break;
}
console.log(i);
var start = new Date();
while ((new Date() - start) < 250);
}
}
function set_mousedown_condition {
$("#mousedown_button").mouseover(function() {
count_flag = false;
}).mouseleave(function() {
count_flag = false;
}).mousedown(function() {
count_flag = true;
for_loop(); // start the loop also here instead of override the binding
}).mouseup(function() {
count_flag = false;
});
}
// initialize everything once
set_mousedown_condition()

JS How make that function could not be start earlier than 10 seconds after the previous launch?

function testworking(n){
if(n == 1)
testuser();
else
testconfig();
}
setInterval(function(){testworking(n)}, 1000);
How do I make that function testuser(); could not start earlier than 10 seconds after the previous launch?
P.S.:
an approximate algorithm:
if(n == 1){
if (first run function `testuser()` ||
time after previous run `testuser();` == 10 seound){
testuser();
}
}
Set a flag using a timer:
var is_waiting = false;
function testuser() {
if (!is_waiting) {
//do your stuff here
} else {
alert('You must wait ten seconds before doing this again');
}
is_waiting = true;
setTimeout(function() {is_waiting = false}, 10000);
}
You can do it like this
var i = 0;
function testworking(i){
if(i < 10) {
console.log(i);
} else {
console.log('Here is 10 second');
}
}
setInterval(function(){
i = (i == 10) ? 0 : i;
i++;
testworking(i);
}, 1000);
It's not entirely clear what you're looking for, but here's something that might give you an idea.
var n = 1;
var testUserInterval;
function testworking(n) {
if (n == 1)
testuser();
else
testconfig();
}
function testuser() {
var cnt = 0;
if (testUserInterval == null) {
testUserInterval = setInterval(function() {
document.getElementById("testusercnt").innerHTML = cnt;
cnt += 1;
if (cnt == 10) {
clearInterval(testUserInterval);
testUserInterval = null;
//DO SOMETHING ???
testuser();
}
}, 1000);
}
}
function testconfig() {
document.getElementById("testconfig").innerHTML = n;
}
setInterval(function() {
testworking(n++)
}, 1000);
testuser cnt:<span id="testusercnt"> </span>
<br/>testconfig n: <span id="testconfig"> </span>

clearInterval is not clearing

Take a look at the function below, It purpose is to change the button text
to "Abort", "Abort 0", "Abort 1" and so on.
Once the counter reaches 10 another function should be executed, but if
the button is clicked, the counter should stop, and the button text should return
to it's original value ("Sync DB").
It seems I'm trying to clear out the interval in a wrong way.
Any assistance will be appreciated.
function sync_database(abort)
{
if (abort == true) { sync_db_btn.innerHTML = "Sync DB"; return false }
sync_db_btn.innerHTML = "Abort"
var i = 0;
sync_db_btn.addEventListener("click", function() { sync_database(true) } );
var x = setInterval(function() {
if (abort == true) {
clearInterval(x);
}
if (i < 10) {
sync_db_btn.innerHTML = "Abort " + i++;
}
}, 1000);
}
var x;
sync_db_btn.addEventListener("click", function() {
sync_database(true);
clearInterval(x);
} );
function sync_database(abort)
{
if (abort == true) { sync_db_btn.innerHTML = "Sync DB"; return false }
sync_db_btn.innerHTML = "Abort"
var i = 0;
x = setInterval(function() {
if (i < 10) {
sync_db_btn.innerHTML = "Abort " + i++;
}
}, 1000);
}
I think you need something like this:
var sync_db_btn = document.getElementById('but'),
abortSync = -1,
interval,
sync_database = function () {
var i = 0;
abortSync *= -1;
if (abortSync < 0) {
sync_db_btn.innerHTML = 'Sync DB';
clearInterval(interval);
return false;
}
sync_db_btn.innerHTML = 'Abort';
interval = setInterval(function () {
if (i < 10) {
sync_db_btn.innerHTML = 'Abort ' + i++;
} else {
sync_db_btn.innerHTML = 'Sync DB';
clearInterval(interval);
abortSync = -1;
}
}, 1000);
};
sync_db_btn.addEventListener('click', sync_database);
A live demo at jsFiddle.

Categories