python 3.x - flask_moment current time refresh in browser not updating -
following basic setup python3.4, jinja2, flask_moment (0.5.1), flask(0.10.1) integration. trying out capability understand workings. example nonsensical want browser display:
- firstly, actual time in real time ("the local time ...") continually updated in real time
- secondly, time elapsed ("that ...").
jinja2 code:
<p>the local time {{ moment(current_time).format('lt', refresh=true) }}.</p> <p>that {{ moment(current_time).fromnow(refresh=true) }}</p>
the second line refresh works fine in browser ("that 1/2/3 minutes ago"), indicating libraries working correctly, first line refresh doesn't work.
the method moment(current_time).format('lt', refresh=true) thought display time in real time refresh option set "true", doesn't.
looking @ browser markup, libraries there , there no errors. see markup below. hints appreciated or maybe misunderstanding capability?
browser source:
<div class="container"> <div class="page-header"> <h1>hello, flask!</h1> </div> </div> <p>the local time <span class="flask-moment" data-timestamp="2016-01-28t12:19:20z" data-format="format('lt')" data-refresh="60000" style="display: none">2016-01-28t12:19:20z</span>.</p> <p>that <span class="flask-moment" data-timestamp="2016-01-28t12:19:20z" data-format="fromnow(0)" data-refresh="60000" style="display: none">2016-01-28t12:19:20z</span></p> <script src="/static/bootstrap/jquery.min.js?bootstrap=3.3.5.7"></script> <script src="/static/bootstrap js/bootstrap.min.js?bootstrap=3.3.5.7"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment-with-locales.min.js"></script> <script> moment.locale("en"); function flask_moment_render(elem) { $(elem).text(eval('moment("' + $(elem).data('timestamp') + '").' + $(elem).data('format') + ';')); $(elem).removeclass('flask-moment').show(); } function flask_moment_render_all() { $('.flask-moment').each(function() { flask_moment_render(this); if ($(this).data('refresh')) { (function(elem, interval) { setinterval(function() { flask_moment_render(elem) }, interval); })(this, $(this).data('refresh')); } }) } $(document).ready(function() { flask_moment_render_all(); });
this misunderstanding on how flask-moment works, due not clear documentation.
the moment(current_time).format('lt', refresh=true)
expression put in jinja template runs in server, not browser. in particular, current_time
variable has fixed value presume assigned in python code.
the result of running expression javascript code runs on browser. when pass refresh=true
, javascript code runs every minute. while code run repeatedly, rendered time same, because value current_time
set on server , isn't being updated. use of auto-refresh makes sense 1 of "dynamic" rendering options of moment.js, such "x secs/mins ago" format, because change time passes.
if wanted implement clock, updates time, need solution in javascript on browser.
Comments
Post a Comment