javascript - 当满足特定条件时停止函数并在其他条件下再次启动它

标签 javascript jquery return

我有一个复选框(有点开关按钮)来打开/关闭功能。当我打开它时,功能应该工作,当我关闭它时,功能不应该工作。

现在的问题是当我打开开关,运行该功能并将其重新关闭时,该功能仍在工作并且没有停止。当开关关闭时,我需要停止该功能的工作。我做了一个fiddle也是我的问题。

HTML 结构:

<label for="onoffswitch">Switch: </label>
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="mathswitch">
<br><br>
<textarea name="question1" id="question1" class="question_aptitude maths_editor"></textarea>
<br><br>
<div id="result"></div>



JS:

var isMaths=0;
$(function(){
    $("#mathswitch").on('click', function(e){
        chk= $("#mathswitch:checked").length;     
        if (chk>0)
        {
            isMaths=1;
        }
        else
        {
            isMaths=0;
        }
        e.stopImmediatePropagation();
    })

    $(".maths_editor").focus(function(){
        store_id= $(this).prop('id');
        console.log("chk :" + chk);
        if (isMaths=='1')
        {
           myfunction();
        }
        else
        {
          $('#'+store_id).on('keyup', function (e){
            var key = e.keyCode;
            if (key == 77 && e.altKey)
            {
                alert("Maths is turned OFF");
                e.stopImmediatePropagation();
                return;
            }
            if (key == 90 && e.altKey)
            {
                alert("Maths is turned OFF");
                e.stopImmediatePropagation();
                return;
            }
          })
        }
    })
})
function myfunction() {
    $("#result").text("Maths is ON!");
  console.log("working!");
  $('#'+store_id).on('keyup', function (e){
    var key = e.keyCode;
    if (key == 77 && e.altKey)
    {
        alert("Maths is turned ON");
        e.stopImmediatePropagation();
        return;
    }
    if (key == 90 && e.altKey)
    {
        alert("Maths is turned ON");
        e.stopImmediatePropagation();
        return;
    }
  })
}



我在 google 上搜索过有关停止某个函数的信息,大多数人都说使用 return 但这似乎不适用于我的情况。

最佳答案

我已经修改了您的 JavaScript,以获得我认为您想要的功能,here's the fiddle .

主要问题是,每次编辑器聚焦或运行 myFunction 时,您都会添加一个 keyup 事件,并且您还尝试在无法记录的范围内记录 chk 变量。我的修改改变了您希望它“打开或关闭功能”的核心方面,但我觉得它可能适合您需要的功能。如果您需要我解释任何内容,请随时发表评论!

代码如下:

var isMaths = false;

$(function() {
    $("#mathswitch").on('click', function(e) {
        isMaths = $("#mathswitch").prop('checked');

        e.stopImmediatePropagation();
    })

    $('.maths_editor').on('keyup', myfunction);
})

function myfunction(e) {
    var key = e.keyCode;

    if (isMaths) {
        $("#result").text("Maths is ON!");
        console.log("working!");

        // Here is where you would call the function that's in an
        // external file as you said in the comments on your question.
        externalFileFunctionCall();

        if (key == 77 && e.altKey) {
            alert("Maths is turned ON");
            e.stopImmediatePropagation();
            return;
        }
        if (key == 90 && e.altKey) {
            alert("Maths is turned ON");
            e.stopImmediatePropagation();
            return;
        }
    } else {
        $("#result").text("Maths is OFF!");

        if (key == 77 && e.altKey) {
            alert("Maths is turned OFF");
            e.stopImmediatePropagation();
            return;
        }
        if (key == 90 && e.altKey) {
            alert("Maths is turned OFF");
            e.stopImmediatePropagation();
            return;
        }
    }
}

关于javascript - 当满足特定条件时停止函数并在其他条件下再次启动它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35170352/

相关文章:

java - 如何在 Java 中返回而不是中断

javascript - 加载到页面后使用图像

javascript - 输入正确密码后如何重定向?

jquery - 使用 jquery 删除一个类并在单击按钮时隐藏一个 div

javascript - 如何将未处理的按键发送到特定的输入字段?

java - 返回格式化字符串无法正常工作

javascript - react onClick 问题

javascript - 根据层次结构重置选择下拉列表

jQuery.html() 将删除选择选项的 selected 属性

php - 如何打印从 Php 中的函数返回的数组的所有元素?