javascript - 设置显示时长

标签 javascript google-chrome-extension

我有一个 HTML 代码,例如

<button id="save" onclick="save_options()">Save</button>

<div id="status"></div>` 

和一段 javascript,例如

function save_options() {
  var api = document.getElementById('wptk').value;
    chrome.storage.sync.set({
    savedApi: api,
  }, function() {
    // Update status to let user know options were saved.
    var status = document.getElementById('status');
    status.textContent = 'OK! API Key saved!';
    setTimeout(function() {
      status.textContent = '';
    }, 750);
  });
}

document.getElementById('save').addEventListener('click', save_options);

单击按钮后,div id="status" 会在 750 毫秒后出现一条消息 OK! API key 已保存!。没关系。

但是消息消失得非常快! - 我如何定义消息的显示持续时间?

最佳答案

您将在 750 毫秒后清除状态。将其更改为 5 秒。

setTimeout(function() {
      status.textContent = '';
    }, 5000);

如果您想延迟显示消息,则需要在 750 毫秒后设置消息,设置另一个计时器,然后清除它。

// Update status to let user know options were saved.
var status = document.getElementById('status');

setTimeout(function() {
  status.textContent = 'OK! API Key saved!';
  setTimeout(function() {
    status.textContent = ''
  }, 5000);
}, 750);

关于javascript - 设置显示时长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51745504/

相关文章:

javascript - 在 chrome 扩展程序中打开 url 而不打开选项卡?

javascript - 从 Web 服务 (asmx) 页面引用 asp.net 页面上的字段

javascript - angularjs ng-显示多个div并清理未选择的模型值

javascript - C#中设置Cookie并Jquery中访问

javascript - SVG 圆中 dasharray 属性的奇怪行为

javascript - 如何在 list 版本 2 中将模板与 Backbone.js 一起用于 Chrome 扩展

javascript - 更改类时淡入淡出的流畅过渡

javascript - 无法通过 Google Chrome 扩展中的 YouTube JS API 访问 YouTube 视频,但可以在 JS Fiddle 中访问

google-chrome - 如何修改Chrome ExtensionInstallWhitelist?

javascript - 在隐身模式下使用 chrome.browsingData.remove()