简体   繁体   English

二次点击功能不同

[英]Different Function on Second Click

I've managed to set up a script that changes the characters in a table and changes the colour of certain ones. 我设法建立了一个脚本,该脚本可以更改表中的字符并更改某些字符的颜色。 What I want to do is have different characters and colours on the second click but I have no idea how I would go about it. 我想做的是第二次点击具有不同的字符和颜色,但是我不知道该如何处理。

var isStart = false;
var letterString = "D0E916C0A8CED059359C02DARREN KORB - PALE WATCHERS.mp3E415AAB0014400E104B40DE96A0";
var letters = letterString.split('');
var currentLetter = 0;
var intervalID;

function changeLetter() {
    $("#t-"+currentLetter).text(letters[currentLetter - 1]);
    currentLetter +=1 ;
    if (currentLetter == 23) {
        $("#t-23").css("color", "#d49a9a");   
    }
    if (currentLetter == 24) {
        $("#t-24").css("color", "#d49a9a");             
    }
    if (currentLetter == 25) {
        $("#t-25").css("color", "#d49a9a");             
    }
        if (currentLetter == 26) {
           $("#t-26").css("color", "#d49a9a");             
    }
}


$(document).ready(function() {
    var image = $('#content').click(function() {
        if  (!isStart) {
            isStart = true;
           intervalID =  setInterval(changeLetter, 100);
        }
    });
});

Add a variable to count the number of clicks and then in your click function, increment and check the value to determine what you want to do. 添加一个变量来计算点击次数,然后在点击功能中增加并检查该值以确定您要执行的操作。 Like this: 像这样:

var currentLetter = 0;
var intervalID;
var clickCount = 0; // <-- add this line


if  (!isStart) {
    clickCount++;
    if (clickCount == 1) {
        //do the first click stuff
    }
    if (clickCount == 2) {
        //do the second click stuff
    }
}

If you want to handle more than two clicks, you may want to use a switch statement, and the default case would be for all counts thats you don't specifically handle. 如果要处理两次以上的单击,则可能要使用switch语句,并且默认情况是针对所有未专门处理的计数。

Use jQuery's .one() method. 使用jQuery的.one()方法。 It allows you to catch an event once, then you can do something else with subsequent clicks. 它允许您一次捕获一个事件,然后可以在随后的单击中执行其他操作。

var image = $('#content').one('click' function() {
    // doSomething

    $(this).on('click', function() {
        if  (!isStart) {
            isStart = true;
           intervalID =  setInterval(changeLetter, 100);
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM