I have written a function window.setinterval followed by two if conditions. Working absolutely as I wanted in Firefox, Opera, IE. In webkit engines, the function is not being carried forward.
x = window.setInterval(function() {
if(a.speed >= a.maxSpeed && b.speed >= b.maxSpeed && c.speed >= c.maxSpeed) {
a.stop();
b.stop();
c.stop();
}
if(a.speed === 0 && b.speed === 0 && c.speed === 0 && completed === 3) {
window.clearInterval(x);
enableControl();
printResult();
enableControl();
}
}, 100);
I’ve set an interval where when max.speeds are reached, stop function should be called and If speeds are 0, the result should be displayed. In Mozilla, IE, Opera its working exactly as I wanted. But in webkit, the first if condition itself isn’t being executed. An infinite loop is running.
I’ve tried nested if also, even then the first if condition isn’t being executed. Please help me resolve this issue.
If this isn’t a correct approach, can I trigger a timeout after c.stop() and manually make the speed’s of a,b,c to 0 and then enable control and display result?
This the stop function you asked for:
Slot.prototype.stop = function() {
var _this = this,
limit = 30;
clearInterval(_this.si);
_this.si = window.setInterval(function() {
if(_this.speed > limit) {
_this.speed -= _this.step;
$(_this.el).spSpeed(_this.speed);
}
if(_this.speed <= limit) {
_this.finalPos(_this.el);
$(_this.el).spSpeed(0);
$(_this.el).spStop();
clearInterval(_this.si);
$(_this.el).removeClass('motion');
_this.speed = 0;
}
}, 100);
};
If you’ll excuse an answer that kind of sidesteps your question, there are arguments that you should really not be using
setIntervalanyway (see this and this). The strength of the guarantee regarding the time interval is OS and browser dependent, and therefore not very strong. You’re better off usingsetTimeoutto schedule a function that reschedules itself usingsetTimeoutwhen necessary. If you’re worried about the exactness of the interval, you can always adjust the time interval to real time as you see fit.