2017-04-10 81 views

回答

2

设置一个当函数被调用的时间戳,那么当你想看到多少时间已过,当前时间比较时间戳。像这样的东西会给你经过时间以毫秒为单位:

var startTime; 

function MyFunc(){ 
    startTime = Date.now(); 
    //My code... 
} 
function GetTimeElapsed(){ 
    var elapsedTime = Date.now() - startTime; 
    return elapsedTime; 
} 
+0

谢谢你,@ReedRaymond –

+0

@RakefetZeiman如果这回答了你的问题,请考虑标记为正确的答案。 –

0
function yourFunction() { 
    // Check to see if the variable firstTime has been initialized 
    if (typeof yourFunction.firstTime == 'undefined') { 
     // first time code goes here 
     yourFunction.firstTime = false; //do the initialisation 
    } 
    else{ 
     // non first time code goes here ;) 
    } 
} 
相关问题