Hi all:
I am having difficulties testing window.setInterval function in my Javascript file. Below is the scenario... basically I have a setInterval function in a function which I want to test:
var dummy = false; // global variable not set anywhere else
var INTERVAL_TIME = 20; // global variable not set anywhere else
function myFunction()
{
var id = window.setInterval(function() {
if (...)
{
window.clearInterval(id);
}
else
{
if(...)
{
dummy = true;
}
}
}, INTERVAL_TIME);
}
And I have the following test code in JsTestDriver:
TestMyFunction.prototype.test_myFunction() {
myFunction();
assertTrue(dummy);
}
Everytime the test executes, it fails and says dummy is false, as if the entire setInterval function was never called. I tried playing around with the interval with no success. If I put in an alert in the else clause, it popped up in a fraction of a second and disappeared (and test still fails).
The code works. I have a feeling that it is a timing issue, where the test finishes earlier than the setInterval function, hence complaining that dummy is not set to true. Any suggestions/solutions to this problem?
Thanks.