Making Live Clock javascript

前端 未结 4 995
没有蜡笔的小新
没有蜡笔的小新 2020-12-30 15:44

does anyone know how to make live javascript time running..

i have this php code

    $expiredate = date(\'d m Y G:i:s\', $rdate1);
    $f_ex_date =          


        
4条回答
  •  既然无缘
    2020-12-30 16:33

    It's doable on the client with a little bit of JavaScript. Without using a framework such as jQuery, which would be of marginal help here, the basic method would be something similar to the following:

    • Set up an event handler to fire each second

    Within the event handler:

    • Retrieve the current date and time and format it as desired
    • Update the contents of another element with the new value

    As a concrete example, the following function will set up a simple date/time update with a named element:

    function clock( id ) {
        var target = document.getElementById( id );
        if( target ) {
            var callback = function() {
                var datetime = new Date().toLocaleString();
                target.innerHTML = datetime;
            };
        callback();
            window.setInterval( callback, 1000 );
        }
    }
    

    Note the use of new Date().toLocaleString() to retrieve and format the current date/time; also, the use of window.setInterval() to set up the callback to fire each second.

提交回复
热议问题