简体   繁体   English

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

[英]How to display time/clock using dojo or dijit?

Basically, I just need to display the current time (hh:mm:ss am/pm format) inside an html input text box.基本上,我只需要在 html 输入文本框中显示当前时间(hh:mm:ss am/pm 格式)。 And since this will serve as a clock, the time should continue counting/moving on while it is being displayed.并且由于这将用作时钟,因此时间应在显示时继续计数/移动。

I can do this in plain javascript, but I would like to see a dojo/dijit implementation.我可以在普通的 javascript 中做到这一点,但我希望看到一个 dojo/dijit 实现。

Thanks!谢谢!

Here is an example that you may reuse.这是一个您可以重复使用的示例

You could also implement this on your own, using dojox.timing.Timer .您也可以使用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();

A couple of examples for dojo.date.locale.format . dojo.date.locale.format的几个例子。

Look at the code below:看下面的代码:

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);

for the minimalist approach, you could probably just achieve this with an input control and a setTimeout call to dojo.date.locale.format.对于极简主义方法,您可能只需通过输入控件和对 dojo.date.locale.format 的 setTimeout 调用来实现此目的。 You don't necessarily need DojoX/Dijit abstractions here, though some of the dojox.timing code can be useful.您在这里不一定需要 DojoX/Dijit 抽象,尽管一些 dojox.timing 代码可能很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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