MMStopWatch

stopwatch for JS

by bladnman

HTML

<script src="https://raw.github.com/carhartl/jquery-cookie/master/jquery.cookie.js"></script>
<input type=button id="theButton2" value="run test correct" class="runButton">
<br>
<input type=button id="theButton" value="run test direct" class="runButton">
<div id="log" class="log"></div>

CSS

.runButton {
    width:   125px;
    margin:  20px;
}
.log {
   padding:10px; 
    margin: 20px; 
    border: 1px dotted #ccc; 
    color:#888; 
    font-face: arial; 
    font-size:12px; 
    background: #fbfbfb; 
}

JavaScript

/* ************************************ */
$("#theButton2").live('click', function() {
    runTestCorrect();
});

var item;
function runTestCorrect() {
    MMStopWatch.start("timer 1");
    MMStopWatch.start("timer 2");
    MMStopWatch.start("timer 3");
    setTimeout(delayPrint2, 500);
    setTimeout(delayPrint2, 2500);
    setTimeout(delayPrint2, 3500);
}

function delayPrint2() {
    debug("------------");
    MMStopWatch.stop("timer 2");
    debug("item1 time", MMStopWatch.describe("timer 1"));
    debug("item2 time", MMStopWatch.describe("timer 2"));
    debug("item3 time", MMStopWatch.describe("timer 3"));
    
    MMStopWatch.stop("timer 3");
}


function runTestDirect() {
    debug("test running");
   item = new MMStopWatchItem("Loop Test 1");
    delayPrint();
   setTimeout(delayPrint, 500);
   setTimeout(delayPrint, 1500);
   setTimeout(delayPrint, 2500);
   setTimeout(delayPrint, 4500);
   setTimeout(delayPrint, 64500);
}

function delayPrint() {
    debug("item time", item);
}


var MMStopWatch        =  {
    
    start            : function(name) {
        MMStopWatch.INTERNAL.addItemWithName(name);
    }, 
    stop            : function(name) {
        var item        = MMStopWatch.INTERNAL.getItemWithName(name);
        item.stop();
        MMStopWatch.print(name);
    }, 
    print            : function(name) {
        MMStopWatch.INTERNAL.debug(MMStopWatch.describe(name));
    }, 
    describe            : function(name) {
        var item        = MMStopWatch.INTERNAL.getItemWithName(name);
        if (item != null) {
            if (item.isStopped == true) {
                return item.toString();
            }
            
            else {
                return item.toString() + " (running)"; 
            }
        }
        
        // not found
        else {
            return "No stopwatch named ["+name+"] found"; 
        }
    },
    
    
    INTERNAL        : {
        items        : new Object(),
        
       ...