Since setTimeout crashes in while loops.
I don't know if there is a way to do it but I am trying to make one. This is how it looks so far.
<script>
var send = true;
function sendit()
{
alert("test");
return true;
}
while(true)
{
if(send == true)
{
send = false;
setTimeout(function(){
if(sendit() == true) {
send = true;
}
}, 5000);
}
}
</script>
Is it possible this way?
Since setTimeout crashes in while loops.
I don't know if there is a way to do it but I am trying to make one. This is how it looks so far.
<script>
var send = true;
function sendit()
{
alert("test");
return true;
}
while(true)
{
if(send == true)
{
send = false;
setTimeout(function(){
if(sendit() == true) {
send = true;
}
}, 5000);
}
}
</script>
Is it possible this way?
Share Improve this question asked Sep 4, 2015 at 15:42 Iamk DenokIamk Denok 1631 gold badge5 silver badges13 bronze badges 5-
1
This is perhaps an XY Problem - what are you trying to solve? Your code here will block as soon as you set
send = false
as javascript is single threaded, and it's stuck within thewhile(true)
loop at this point, with no chance for the timeout to ever occur. – James Thorpe Commented Sep 4, 2015 at 15:45 - possible duplicate of : stackoverflow./questions/12996193/… – Anand Dwivedi Commented Sep 4, 2015 at 15:45
- @James Thorpe Well it isn't possible than, oh well... – Iamk Denok Commented Sep 4, 2015 at 15:48
- 1 @IamkDenok: Quite possibly you are just looking for setInterval() / clearInterval() for what you want to achieve. But it's hard to tell since we don't know what you wanted to achieve in your original code. If the answer linked above (by Anand) does not solve your problem, you might want to ask a different question explaining what you wanted to achieve in the first place. – Ma3x Commented Sep 4, 2015 at 15:49
- @Ma3x setInterval() is what I was looking for, setInterval() is basically setTimeout in a while loop, thank you! – Iamk Denok Commented Sep 4, 2015 at 15:55
2 Answers
Reset to default 5You haven't explained what you want your code to do. If you want it to alert "test" every 5 seconds then you need this:
<script>
function sendit()
{
alert("test");
// Call sendit() the next time, repeating
setTimeout(sendit, 5000);
}
// Call sendit() the first time
setTimeout(sendit, 5000);
</script>
No need for a loop, just get the function to schedule itself again.
My understanding is that what you're trying to do is the equivalent of Thread.sleep(5000)
in a language like Java or C#. That functionality does not exist in JavaScript. If you want to do something some amount of time after your function's execution, put it in a timeout, but one way or another, that first function will still plete in the same frame unless you're performing an enormous amount of work.
Currently, your code is setting a timeout on sendit()
a practically-infinite number of times before it returns. Since JavaScript is single threaded, even if 20 seconds passed, it still wouldn't have finished your function and couldn't start looking up timeouts it needs to process. What you should be doing is something like having the inside of the timeout set another timeout, and remove the enclosing while(true)
. That could allow for infinite, periodic behavior as I think you're looking for.