Find elapsed time in javascript

前端 未结 9 577
死守一世寂寞
死守一世寂寞 2020-12-10 03:35

I\'m new to JavaScript and I\'m trying to write a code which calculates the time elapsed from the time a user logged in to the current time.

Here is my code:-

9条回答
  •  青春惊慌失措
    2020-12-10 03:57

    What I found useful is a 'port' of a C++ construct (albeit often in C++ I left show implicitly called by destructor):

    var trace = console.log
    function elapsed(op) {
        this.op = op
        this.t0 = Date.now()
    }
    elapsed.prototype.show = function() {
        trace.apply(null, [this.op, 'msec', Date.now() - this.t0, ':'].concat(Array.from(arguments)))
    }
    

    to be used - for instance:

    function debug_counters() {
        const e = new elapsed('debug_counters')
        const to_show = visibleProducts().length
        e.show('to_show', to_show)
    }
    

提交回复
热议问题