如何使用查询从当前机器时间启动计时器?

时间:2016-02-22 10:32:25

标签: jquery

我正在获取页面加载的当前时间,并且我想从当前时间开始计时器。我怎么能这样做?

这是我目前的剧本:

var loadedmodules: TList<HModule>;

procedure DoUnloadPackage(Module: HModule);
var
  i: Integer;
  M: TMemoryBasicInformation;
begin
  // remove the modules handle from the list 
  loadedmodules.Delete(loadedmodules.IndexOf(Module));

  // maybe there are still more forms loaded from the same package?
  if loadedmodules.IndexOf(Module) <> -1 then
  begin
    { Make sure there aren't any instances of any
      of the classes from Module instantiated, if
      so then free them.  (This assumes that the
      classes are owned by the application) }

    for i := Application.ComponentCount - 1 downto 0 do
    begin
      VirtualQuery(GetClass(Application.Components[i].ClassName), M, SizeOf(M));

      if (Module = 0) or (HMODULE(M.AllocationBase) = Module) then
        Application.Components[i].Free;
    end;
    UnRegisterModuleClasses(Module);
  end;

  // gets always called
  UnLoadPackage(Module);
end;

2 个答案:

答案 0 :(得分:1)

你在找这个吗?

&#13;
&#13;
var dt = new Date();
setInterval(function() {
  dt.setSeconds(dt.getSeconds() + 1);
  var time = pad(dt.getHours(), 2) + ":" + pad(dt.getMinutes(), 2) + ":" + pad(dt.getSeconds(), 2);
  $("#timer").text(time);
}, 1000)

function pad(num, size) {
  var s = num + "";
  while (s.length < size) s = "0" + s;
  return s;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="timer"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以将这三行放在功能中并使用setInterval()

function showTime(){
    //var dt = new Date();
    //var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();

    //var time = [add a ajax call to your server and get the time in string format.]
    $("#timer").text(time);
}
setInterval(showTime,1000);

因此,每秒调用一次函数,并使用当前时间更新div。