Как я могу обновить элемент времени каждые 2 SE c в EJS - PullRequest
0 голосов
/ 16 марта 2020

Я создал панель инструментов. js файл и несколько .e js файлов

Это моя статистика .e js файл

<tr>
    <th scope="row" class="list-group-item d-flex justify-content-between align-items-center" data-toggle="tooltip" title="Duration"> Duration </th>
    <td width="80%"><i class="fa fa-fw fa-music" aria-hidden="true"></i> <span class="col-5 text-nowrap" id="Duration"><% if(player) { %> <%= time %> <% } else { %>00:00 <% }%></span></td>
</tr>

Как мне обновить время? каждые 2 се c

это на приборной панели. js файл

  app.get("/dashboard/:guildID/stats", checkAuth, (req, res) => {
    const guild = client.guilds.get(req.params.guildID);
    let player = client.music.players.get(req.params.guildID);
    let time;
    if (player) time = duration(player.queue[0].duration ).toString();
    if (!guild) return res.status(404);
    const isManaged = guild && !!guild.member(req.user.id) ? guild.member(req.user.id).permissions.has("MANAGE_GUILD") : false;
    if (!isManaged && !req.session.isAdmin) res.redirect("/");
    renderTemplate(res, req, "guild/stats.ejs", {guild , player ,time});
  });

Обновление переменной времени каждый се c так что я могу сделать здесь?

1 Ответ

0 голосов
/ 16 марта 2020

Одним из способов достижения этого является использование внешнего интерфейса JavaScript для запуска сети для получения самой последней информации

const yourInfoSegment = $('#segment');

// Fetching the latest data every 2 seconds
setInterval(async () => {
  try {
    const res = await fetch('YOU-API.com/dashboard/${guidID}/stats');
    if(!res.ok) {
      // Shows user there is a error while fetching data
      return;
    }
    // Updating the DOM with the new HTML code from the API
    const htmlCode = await res.text();
    yourInfoSegment.innerHTML = htmlCode;
  }
  catch (err) {
    // Show some error UI to the user
    console.error(err);
  }
}, 2 * 1000);
...