[turboid] javascript solutions

Home     Contact     Search     About me
Syntax solutions
13 Dec 2011

Changing a variable temporarily

Without jumping back too early

In the real world, changing temporarily the state of a toy car is simple: You give it a tap and it begins to move forwards and stops after a short while. If you want to keep it moving, you could give it a tap with the same force every time before it stops. Changing variables in the same way is not that simple - unless you use a certain Turboid function...

One often wants to make his script change a variable temporarily only, in order to let it get its old value after a certain amount of seconds while keeping the new value until the seconds have elapsed. This not only needs more then one or even a couple of code lines, but gets also uncomfortable if another part of our script tries to do the same thing with the same variable before those seconds have elapsed, because with naive approaches the variable would return to its old value too early although it has been changed again to keep the new value longer. Handling this issue needs a whole bunch of sophisticated code...

Wouldn't it be wonderful to have a simple command which tells the script to change a variable and let it return back to its old value after a certain time except if the same command is used again before that time has elapsed? Yes, it would, and in fact it is: If you either copy and paste the code at the end of this article or use the free Turboid framework you can simply use the following line to change myVariable for 7 seconds to the value 321:

	tempchange("myVariable", 321, 7000);

Now the script can call this function many times again before the time span elapses, and the last time it is called it gives back the old value to the variable after exactly seven seconds. Only remember that myVariable must already have a value before changing it using tempchange(), and the variable name must be written in quotes.

You can even use this function for public object properties:

	myObj.tempchange("myProp", 321, 7000);

This even works from the inner parts of an object, that means you can use this.tempchange("myProp", 321, 7000):

	function Prot(){
		this.go = function(){
			this.tempchange("x", 8, 3000);
		this.x = 5;
	obj = new Prot();
In order to use this enormously useful function you only need to copy and paste the following code into your script or use the Turboid framework...
Object.prototype.tempchange = function(varName, val, msec){ 
	var obj = this;
	if((typeof listeningNumbers[varName])=="undefined")
		listeningNumbers[varName] = 0;
	if((typeof obj[varName+"_origVal"])=="undefined")
		obj[varName+"_origVal"] = obj[varName];
	obj[varName] = val;
			obj[varName] = obj[varName+"_origVal"];
}; listeningNumbers = {}; var ieService = {   }; window.tempchange = ieService.tempchange;


Guest wrote on 2012-03-06 at 17:21:57 h:

That's a really important function.

Add comment:

Name: (required)
E-Mail: (required, remains invisible)
Please check all (anti-spam):