Javascript JS删除和添加类计时功能

Javascript JS删除和添加类计时功能,javascript,jquery,html,css,Javascript,Jquery,Html,Css,下面是一些js,我不知道如何修改它来实现我想要的。我需要在延迟40秒后激活js,而不是onclick // retrieve the element element = document.getElementById("ani"); // reset the transition by... element.addEventListener("click", function(e){ e.preventDefault; // -> removing the class element.

下面是一些js,我不知道如何修改它来实现我想要的。我需要在延迟40秒后激活js,而不是onclick

// retrieve the element
element = document.getElementById("ani");

// reset the transition by...
element.addEventListener("click", function(e){
e.preventDefault;

// -> removing the class
element.classList.remove("delay-1");

// -> triggering reflow /* The actual magic */
// without this it wouldn't work. Try uncommenting the line and the transition won't be         retriggered.
element.offsetWidth = element.offsetWidth;

// -> and re-adding the class
element.classList.add("delay-1");
}, false);
用于在指定的毫秒数后调用方法/任务

setTimeout(function() {
    //your code/function here
}, (40*1000)); //40 seconds

使用函数setTimeout

如图所示

就你而言:

// retrieve the element
element = document.getElementById("ani");

// reset the transition by...
element.addEventListener("click", function(e){
  e.preventDefault;

  //introducing a delay of 40 seconds.
  setTimeout(function() {
    // -> removing the class
    element.classList.remove("delay-1");

    // -> triggering reflow /* The actual magic */
    // without this it wouldn't work. Try uncommenting the line and the transition won't be         retriggered.
    element.offsetWidth = element.offsetWidth;

    // -> and re-adding the class
    element.classList.add("delay-1");
  }, 40 * 1000);
}, false);

使用setTimeout函数:

setTimeout(function() {
    // retrieve the element
    element = document.getElementById("ani");

    // reset the transition by...
    element.addEventListener("click", function(e){
    e.preventDefault;

    // -> removing the class
    element.classList.remove("delay-1");

    // -> triggering reflow /* The actual magic */
    // without this it wouldn't work. Try uncommenting the line and the transition won't be retriggered.
    element.offsetWidth = element.offsetWidth;

    // -> and re-adding the class
    element.classList.add("delay-1");
    }, false);
}, 40000);

只需使用一个.doi remove:“element.addEventListener”(“click”,function(e){“我几乎有0个js知识是的,
e.preventDefault()
让它工作起来了,非常感谢!+1,最后一个问题,我能让它每40秒重复一次吗?我有多个div也可以这样做,但时间相同。