All proper programming languages I can think of have a real simple function – sleep(). It suspends the execution of your program for a time. This is what I wanted to do. Having no choice but to use javascript in this case, I went around looking for what the sleep() function is called in this dreadful language.
Turns out there is none. There is only setTimeout() and setInterval(). Either will call a function after a time, but in between your code will continue to execute.
All I could find on the internet is a bunch of solutions to this problem involving an infinite loop. That, for I hope obvious reasons, was not acceptable for me.
I considered using `yield` but that doesn’t work for some reason in my version of the current firefox trunk. I did the javascript version thing, but then none of the javascript I had (yield or not) would work at all.
So I grumbled for a couple of hours and finally figured out a solution so my stuff still works as I want it, and all that’s lost is readability. I’ll share this beast with you all in case you ever run into the same problem.
This is what I wanted done in the first place, overly simplfied (lack of whitespace courtesy of wordpress):
function wish(i, j)
{
var k = i + j;
for(var m = 0; m < 10; m++)
{
// start doing something outside of my control
sleep(5000); // sleep for 5 seconds
// check the results of the something
alert(k + “, ” + m + results);
}
}
Like I said, it’s overly simplified. There’s no need to bring 3D and benchmarks into this example.
Since setInterval() is all I have to work with, that’s what I’ll use. The tricks to address my concerns are:
- Make a global function that will take the (i, j) parameters and have it call another function (real()) that doesn’t take any parameters
- Copy the parameters to wish() into glabal variables
- Where the sleep() would be have a setInterval(‘real()’, 5000) instead
- Copy all the local variables in real() to global variables before the setInterval()
- Add a condition so real() will know whether it should continue from where it left off or start from the beginning.
A lot of work, but the other option is to not do it at all.. Here’s what it now looks like:
var gI;
var gJ;
function wish(i, j)
{
gI = i;
gJ = j;
real();
}
var gK;
var gM = 0;
var didSleep = false;
function real()
{
if (didSleep)
{
// check the results of the something
alert(gK + “, ” + (gM – 1) + results);
}
else
{
gK = gI + gJ;
}
for(m = gM; m < 10; m++)
{
// start doing something outside of my control
gK = k;
gM = m + 1;
didSleep = true;
setInterval(“real()”, 5000);
return;
}
}
That’s pretty disgusting. I’d blame the people who came up with javascript if it wasn’t for the fact that javascript was never designed to be a proper language. Javascript was made for people one level above HTML, which is far, far from real programming and it served those people well.
Instead I blame the people responsible for the javascript hype. Both the fanboys who don’t know any better but their strength is in numbers; and the programmers who sell their time/ideas by proclaiming that AJAX is the only way to go. I even blame google a little for making the two useful javascript applications in the world.
I’ll say it again – disgusting.