Jump to content

variable setInterval


M.O.S. Studios

Recommended Posts

Hey guys,

    Basically i wanted to make a function that is like setInterval but can have the interval change.

this is what i've got so far.

 

window.setVariableInterval = function(callbackFunc, timing){
	var variableInterval = {
		firstInterval: timing,
		interval: timing,
                        callback: callbackFunc,
                        clear: "",
		placeHolder: function(){return variableInterval.clear = setInterval(
                            function(){
                                if(variableInterval.interval == variableInterval.firstInterval){
                                    variableInterval.callback.call(variableInterval);
                                }else{
                                    clearInterval(variableInterval.clear);
                                    this.placeHolder();
                                }
                            }, variableInterval.interval)
                        }
	}
	return variableInterval.placeHolder();
};

var that = setVariableInterval(function(){this.interval++; document.getElementById('test').innerHTML = this.interval+ " - " +this.firstInterval}, 1);

 

 

the following example shows a script that continuously counts up, and slows down by 1 millisecond each time.

 

So far I have it working fairly well. Interval keeps going, and stops when the interval changes. my problem occurs when I try and get it to restart at the new interval rate.

that process happens in between the asters.

 

 

thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/213933-variable-setinterval/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.