简体   繁体   中英

JavaScript: Carrying Time Variable Across Multiple Functions

I'm trying to get the time time difference between when a function executes, inside a second function.

However I'm note sure what the best way of doing it is. I was considering a global variable, however I'm not sure how that would work considering the variable needed in the second function is the time the first function executed.

Here's my code:

var d = new Date(); 

function myFunction(A) {
   // execute function A
   var functionAExecuted = d.getMilliseconds();

}

function myFunction(B) {
   // Execute function B
   var functionBExecuted = d.getMilliseconds();
   var diff = functionAExecuted - functionBExecuted;

   if (diff > 3000) {
   //do something
   }


}

It won't execute because functionAExecuted won't carry to the second function. What's the best way of doing this?

You've got a couple things going on here:

  1. For starters, you are using the same Date object to measure the start times for each function. The problem with this is that a Date object represents a fixed state, namely the exact moment in time when it was instantiated. As a result two separate calls to getMilliseconds() will return the same value, regardless of when they are called
  2. Your two functions are attempting to access each others' local variables, which, as I'm sure you know, is impossible unless they pass them to each other as arguments.
  3. You are redefining your myFunction before you actually run myFunction(A) . This due to the fact that JavaScript named function declarations are hoisted (along with their definitions) to the top of the enclosing context, meaning that the functionality of myFunction(A) will always be replaced by that of myFunction(B) before you have the chance to call either function. I would advise you to name each function separately and then conduct your test.

As an alternative, consider renaming your functions and using the outer scope to hold the two times and calculate the difference after the fact:

var times = {};

function myFunctionA(a) {
  times.functionA = new Date();
  // do something
}

function myFunctionB(b) {
  times.functionB = new Date();

  var diff = times.functionB.getMilliseconds() - times.functionA.getMilliseconds();

  if (diff > 3000) {
    //do something else
  }
}

I would store the first value in a form, then retrieve it in the second function:

var d = new Date(); 

function myFunction(A) {
   var functionAExecuted = d.getMilliseconds();
   // do something
   document.forms.myForm.timeStorageFunctionA.value = functionAExecuted;
}

Now d.getMilliseconds() is accessible:

function myFunction(B) {
    var functionBExecuted = new Date().getMilliseconds();
    var functionAExecuted = document.forms.myForm.timeStorageFunctionA.value;
    var diff = functionAExecuted - functionBExecuted;

    if (diff > 3000) {
        //do something else
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM