如何使用dojo或dijit显示时间/时钟

How to display time/clock using dojo or dijit?

本文关键字:时间 时钟 显示 dijit 何使用 dojo      更新时间:2023-09-26

基本上,我只需要在html输入文本框中显示当前时间(hh:mm:ss am/pm格式)。因为这将作为一个时钟,时间应该继续计数/移动,当它被显示。

我可以在纯javascript中做到这一点,但我想看到一个dojo/dijit实现。

谢谢!

这是一个您可以重用的示例。

您也可以使用dojox.timing.Timer自己实现它。

dojo.require('dojox.timing');
t = new dojox.timing.Timer(1000);
t.onTick = function() {
   //One second elapsed
   var now = new Date();
   //format now using dojo.date.locale.format
   //update your text box with the result
}
t.onStart = function() {
   //Do whatever setup is needed
}
t.start();

dojo.date.locale.format.

对于最简单的方法,您可能只需要通过输入控件和对dojo.date.locale.format的setTimeout调用来实现这一点。在这里,您不一定需要DojoX/Dijit抽象,但是需要一些DojoX。计时代码是有用的。

看下面的代码:

var myTime = new dijit.form.TimeTextBox({
    value: new Date()
    constraints: {
        timePattern: 'hh:mm:ss a',
    },
    id: "timeTb"
}, dojo.byId("timeDiv"));
var elem = document.getElementById("timeTb");
setInterval(function(){
    dijit.byId("timeTb").setValue(new Date());
}, 1000);