简体   繁体   中英

How to create a live running clock using web2py?

I'd like to create a live running clock on my website built with web2py.

I have a get_current_time function in my controller that returns the current time and displays it on my page. However, the function is only called once when the page is loaded, so it only shows the time of when the page was loaded. I'd like this function to be called every minute so that the current time is always shown on the page.

Controller(default.py)

from datetime import datetime
def get_current_time():
    return str(datetime.now())

def index():
    return dict(message=get_current_time())

View (index.html):

{{=message}}

When I go to my applications web2py url: http://127.0.0.1:8000/myapplication/default/index

I see the following, but the time does not change or update after loading:

'2017-08-31 07:28:17.635000

I've tried to utilize JavaScript to get a live clock with the following:

<!DOCTYPE html>
<body>
<p id='current_time'>
    </p>
<script>
document.getElementById("current_time").innerHTML = {{=message}}

setInterval(function() {
document.getElementById("current_time").innerHTML = {{=message}}
},1000)
</script>
  </body>
</html>

I've also tried the following:

<!DOCTYPE html>
<body>
<p id='current_time'>
    </p>
<script>
function httpGet(theUrl)
{
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            return xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", theUrl, false );
    xmlhttp.send();  
}
document.getElementById("current_time").innerHTML =httpGet("http://127.0.0.1:8000/myapplication/default/get_current_time"); =

setInterval(function() {
document.getElementById("current_time").innerHTML = =httpGet("http://127.0.0.1:8000/myapplication/default/get_current_time");
},1000)
</script>
  </body>
</html>

Note: When I visit http://127.0.0.1:8000/myapplication/default/get_current_time , I see the current time in the browser

It's important to note that my goal is to get the current time by calling my get_current_time() function in my controller and not utilize javascript to produce the time. In other words, I'd like javascript to call my python function to get the time.

You'll have to use an Ajax request, but your Ajax code is incorrect. The simplest method is to use the web2py ajax() function:

<script>
setInterval(function() {
  ajax('{{=URL('default', 'get_current_time')}}', [], 'current_time');
}, 1000);
</script>

Of course, this is a very inefficient way to display a clock in the browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM