【问题标题】:JS: how to execute a function after a button is pressed n times?JS:按下按钮n次后如何执行功能?
【发布时间】:2018-07-17 08:27:55
【问题描述】:

假设我必须创建一个登录系统,只有在按下按钮 n 次后才会登录,在 Javascript 中,我认为这并不容易,因为没有常用的方法。
谁能提供线索?

var ctr = 0;
function cnt() {
ctr++;
if(ctr==5) {
alert("did it");
}
}
<button onClick="cnt()">click me</button>

【问题讨论】:

  • 你试过什么?我们能看到吗? (只需创建一个每次点击都会计数的变量。如果达到数字并执行您的操作,请使用条件)
  • 每次单击按钮时,都会增加一个计数器。当计数器达到阈值时,您会触发给定的行为。
  • 将计数存储在本地存储或增加计数,启用登录
  • 我们可以有一些代码形式的上下文吗?
  • @AhmedBajra 是的,现在看

标签: javascript function button execution


【解决方案1】:

也许像这样https://jsfiddle.net/opcbnswe/2/

$('#btn').on("click", function() {
    $.data(this, "counter", ($.data(this, "counter") || 0) + 1);
    if ($.data(this, "counter") === 5) {
        alert("5 times");
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 2020-07-07
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多