如何在同一事件中调用两个函数?

时间:2019-05-17 00:37:45

标签: javascript

我想通过单击除id中间的中间一个div以外的任何div,在类class为box的八个div中显示第一个数组的八个元素,并在中心div中显示第二个数组的元素。问题在于名为nextElems&arr的两个函数不能同时调用。如果调用一个函数,则另一个函数不起作用。我可以调用函数arr,但是如何在单击任何div时同时调用函数nextElems。 您的建议总是非常有用,对我很有帮助。

library(dplyr)
txt %>% filter(grepl("hunter",token_text, ignore.case=TRUE))

我尝试过

   <div id="container"> </div> 

   var words = [40];

    // Showing 8 elements  each time in different divs

    var count = 0;

    var x = "";

    function nextElems() {

        var newArray = words.slice(count, count + 8);

        for (i = 0; i < newArray.length; i++)

        {
            x += '<div class=box>' + newArray[i] + '</div>';

            document.getElementById('container').innerHTML = x;
        }

        x = "";

        count += 8;

    //  Creating div in middle between fourth and fifth div

        if (count <= 40) {

            var center = document.querySelector('#div-3');

            center.insertAdjacentHTML('afterend', '<div id="center"> </div>')

        }

    }

    nextElems();

    //  Showing elements one at a time in order

    var oppo = ["White", "Easy", "Soft", "Low", "Good"];

    var x = "";

    var count = 0;

    function arr() {

        if (count < oppo.length) {

            x += '<div>' + oppo[count] + '</div>';

            document.getElementById('center').innerHTML = x;

            count++;

        } else {

            count = 0;

            document.getElementById('center').innerHTML = "";

        }

        x = "";

    }

    arr();

    $('.box').click(function() {

        arr();

    });


我也尝试过单击按钮和其他jQuery     方法。

1 个答案:

答案 0 :(得分:0)

您需要在函数范围内声明变量。 因为现在您的代码按以下顺序执行:

 {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "/mnt/c/Users/David/Documents/code/puc-rio/inf1010/lab8/teste.out",
            "args": [],
            "stopAtEntry": true,
            "cwd": "/mnt/c/Users/David/Documents/code/puc-rio/inf1010/lab8/",
            "environment": [],
            "externalConsole": true,
            "windows": {
                "MIMode": "gdb",
                "miDebuggerPath": "/usr/bin/gdb",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ]
            },
            "pipeTransport": {
                "pipeCwd": "",
                "pipeProgram": "c:\\windows\\sysnative\\bash.exe",
                "pipeArgs": [
                    "-c"
                ],
                "debuggerPath": "/usr/bin/gdb"
            },
            "sourceFileMap": {
                "/mnt/c": "${env:systemdrive}/",
                "/usr": "C:\\Users\\David\\AppData\\Local\\Packages\\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\\LocalState\\rootfs\\usr"
            }
        }
    ]

所以您需要像

那样声明它
Unable to perform this action because the process is running.

然后您可以使用

var count = 0;
var x = "";
nextElems();
var oppo = ["White", "Easy", "Soft", "Low", "Good"];
var x = "";
var count = 0;
..
someday later ...
..
arr(); //here you modify your global variables "x" and "count", so next function will use it with unpredictable values
nextElems();