首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >只有在x秒内没有鼠标单击时才触发函数。

只有在x秒内没有鼠标单击时才触发函数。
EN

Stack Overflow用户
提问于 2017-11-23 02:41:01
回答 1查看 56关注 0票数 0

我有以下项目:https://sporedev.ro/pleiade/lobby.html (忽略声音,它们在将来不会自动播放)

我经常使用JS突出显示可点击的区域地图,如下所示:

代码语言:javascript
复制
function blinkIn() {
      setTimeout(function() {
        $("#map area").filter(':eq(0), :eq(1), :eq(2), :eq(3), :eq(4), :eq(5), :eq(6), :eq(7), :eq(8), :eq(9), :eq(10), :eq(11), :eq(12), :eq(13), :eq(14)').trigger("mouseenter.mapify").trigger("focus.mapify").trigger("touchend.mapify");
        $(".mapify-svg polygon").filter(':eq(0), :eq(1), :eq(2), :eq(3), :eq(4), :eq(5), :eq(6), :eq(7), :eq(8), :eq(9), :eq(10), :eq(11), :eq(12), :eq(13), :eq(14)').css("stroke", "#FFF").css('opacity', '0.5').css('stroke-dasharray', '50').css('stroke-dashoffset', '100').css('animation', 'dash 1.5s linear forwards');
        blinkOut();
      }, 3000);
    }

    function blinkOut() {
      setTimeout(function() {
        $("#map area").filter(':eq(0), :eq(1), :eq(2), :eq(3), :eq(4), :eq(5), :eq(6), :eq(7), :eq(8), :eq(9), :eq(10), :eq(11), :eq(12), :eq(13), :eq(14)').trigger('mouseout.mapify');
        $(".mapify-svg polygon").filter(':eq(0), :eq(1), :eq(2), :eq(3), :eq(4), :eq(5), :eq(6), :eq(7), :eq(8), :eq(9), :eq(10), :eq(11), :eq(12), :eq(13), :eq(14)').css("stroke", "").css('opacity', '').css('stroke-dasharray', '').css('stroke-dashoffset', '').css('animation', '');;
        blinkIn();
      }, 1500);
    }

    blinkIn();

我做了一些研究,例如,如何防止功能运行,如果鼠标点击被检测到。我发现了mousedown和其他州的情况,但这似乎帮不了我。

代码语言:javascript
复制
document.body.getElementsByTagName("*").onclick = function(){ clearTimeout; };

只有在x秒内没有鼠标单击时,我才能触发blinkIn函数?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-23 02:47:54

你能试试这个吗?(但没有测试)

代码语言:javascript
复制
var timeout;
function blinkIn() {
   timeout = setTimeout(function() {
    $("#map area").filter(':eq(0), :eq(1), :eq(2), :eq(3), :eq(4), :eq(5), :eq(6), :eq(7), :eq(8), :eq(9), :eq(10), :eq(11), :eq(12), :eq(13), :eq(14)').trigger("mouseenter.mapify").trigger("focus.mapify").trigger("touchend.mapify");
    $(".mapify-svg polygon").filter(':eq(0), :eq(1), :eq(2), :eq(3), :eq(4), :eq(5), :eq(6), :eq(7), :eq(8), :eq(9), :eq(10), :eq(11), :eq(12), :eq(13), :eq(14)').css("stroke", "#FFF").css('opacity', '0.5').css('stroke-dasharray', '50').css('stroke-dashoffset', '100').css('animation', 'dash 1.5s linear forwards');
    blinkOut();
  }, 3000);
}

function blinkOut() {
   setTimeout(function() {
    $("#map area").filter(':eq(0), :eq(1), :eq(2), :eq(3), :eq(4), :eq(5), :eq(6), :eq(7), :eq(8), :eq(9), :eq(10), :eq(11), :eq(12), :eq(13), :eq(14)').trigger('mouseout.mapify');
    $(".mapify-svg polygon").filter(':eq(0), :eq(1), :eq(2), :eq(3), :eq(4), :eq(5), :eq(6), :eq(7), :eq(8), :eq(9), :eq(10), :eq(11), :eq(12), :eq(13), :eq(14)').css("stroke", "").css('opacity', '').css('stroke-dasharray', '').css('stroke-dashoffset', '').css('animation', '');;
    blinkIn();
  }, 1500);
}

blinkIn();
$(document).click(function(){
  clearTimeout(timeout);
  blinkIn();
});

传递给一个变量setTimeout,然后当用户单击时,只需在变量中使用clearTimeout

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47446738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档