subtract values of 2 variables

0

How can I subtract the values obtained by pressing these 2 buttons ?, that is, subtract the "start" to the "end" and get the milliseconds.

var inicio:int;
var fin:int;
var milisec1:int;
var milisec2:int;

btn1.addEventListener(MouseEvent.CLICK,click1);
function click1(e:MouseEvent):void{
  var milisec1:Date = new Date();
  var inicio = milisec1.getTime();
  salida1.text = inicio;
}

btn2.addEventListener(MouseEvent.CLICK,click2);
function click2(e:MouseEvent):void{
  var milisec2:Date = new Date();
  var fin = milisec2.getTime();
  salida2.text = fin;
}
    
asked by Lalo 27.04.2017 в 18:58
source

1 answer

0

It's a problem with the Scope, by declaring twice your variables one remains as Global variable and the other as Local within the function, so when you assign a value to milisec1 it is only accessible from the click1 function, so which when you try to subtract it would mark you that millisec1 is NULL

var startTime: Date;
var endTime: Date;

btn1.addEventListener(MouseEvent.CLICK, click1);

function click1(e: MouseEvent): void {
 startTime = new Date();
 var inicio = startTime.getTime();
 salida1.text = inicio;
}

btn2.addEventListener(MouseEvent.CLICK, click2);

function click2(e: MouseEvent): void {
 endTime = new Date();
 var fin = endTime.getTime();     
 salida2.text = fin;
 var total:int = Math.floor(endTime.valueOf() - startTime.valueOf());
 trace(total);
}
    
answered by 27.04.2017 в 19:43