简体   繁体   English

页面准备就绪时功能未运行

[英]Function not running at page ready

I have code set like this: 我有这样的代码集:

$(document).ready(function() {
     select_a_color();
});

function select_a_color(){
     var myColors = ["#441540",
     "#20A923",
     "#EF5918",
     "#E33EF3",
     "#C6452B",
     "#F94B34",
     "#236BD4",
     "#51A224",
     "#6E3372"];
     var gencolor = myColors[Math.round(Math.random() * (myColors.length - 1))];
     alert(gencolor);
}

Each time I call the function, it's supposed to change gencolor and (for test) alert with the value for gencolor . 每次调用该函数时,都应该更改gencolor并使用gencolor的值(用于测试) gencolor alert I need this to run when my page loads initially, as an AJAX query needs the color, but I can't get it to run when my page loads. 我需要在页面初始加载时运行它,因为AJAX查询需要颜色,但是在页面加载时却无法使其运行。 Note that when it's not a function, gencolor is generated correctly. 请注意,当它不是函数时,将正确生成gencolor

I end up with errors that gencolor is not defined. 我最终遇到未定义gencolor的错误。

What am I missing? 我想念什么?

UPDATE EDIT : Made some changes, and I get the function to run, but it won't pass the variable gencolor correctly. 更新编辑 :进行了一些更改,并且我让该函数运行,但是不会正确传递变量gencolor

var gencolor;
function selectColor(gencolor){
    var myColors = ["#441540",
        "#20A923",
        "#EF5918",
        "#E33EF3",
        "#B46BF9",
        "#D95AA3",
        "#622469",
        "#FB5AE8",
        "#51A224",
        "#6E3372"];
    var gencolor = myColors[Math.round(Math.random() * (myColors.length - 1))];
    alert(gencolor); //THIS WORKS!
}

function load_test_map2(gencolor) {
    selectColor();//THIS SUCCESSFULLY TRIGGERS THE FUNCTION AND ALERT WORKS
    console.log(gencolor); //THIS IS UNDEFINED
}

I'm trying to pass the gencolor variable into this other function, but it's obviously not working. 我正在尝试将gencolor变量传递到此其他函数中,但显然不起作用。

<!DOCTYPE html>
<html>
    <head>
        <title>test</title>
    </head>
    <body>


        <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
        <script>
            $(document).ready(function() {
                select_a_color();
            });

        function select_a_color(){
        var myColors = ["#441540",
        "#20A923",
        "#EF5918",
        "#E33EF3",
        "#C6452B",
        "#F94B34",
        "#236BD4",
        "#51A224",
        "#6E3372"];
        var gencolor = myColors[Math.round(Math.random() * (myColors.length - 1))];
        console.log(gencolor);
        }
        </script>
    </body>
</html>

Doing this and looking into the console and also refresh the page, I get following outputs: 这样做并查看控制台并刷新页面,我得到以下输出:

#F94B34 #51A224 #E33EF3 #F94B34 .. #F94B34 #51A224 #E33EF3 #F94B34 ..

So it seems to work. 这样看来行得通。 Also do console.log( message ) instead of alert() . 还可以执行console.log( message )而不是alert() Also do you have included jQuery before the script? 您还在脚本之前包含了jQuery吗?

I see in your edit history that the original code is 我在您的修改历史记录中看到原始代码是

function load_test_map2() {
    $(document).ready(function() {
        select_a_color();
    });

function select_a_color(){
var myColors = ["#441540",
"#20A923",
"#EF5918",
"#E33EF3",
"#C6452B",
"#F94B34",
"#236BD4",
"#51A224",
"#6E3372"];
var gencolor = myColors[Math.round(Math.random() * (myColors.length - 1))];
alert(gencolor);
}

There are just two mistakes 只有两个错误

  1. You forgot to close the load_test_map2() function. 您忘记了关闭load_test_map2()函数。 But lets assume you just forgot to copy the closing braces. 但是,假设您只是忘记复制右括号。
  2. You do not have to next these two functions within load_test_map2 but if you do, you need to call it. 您不必在load_test_map2这两个函数,但是如果需要,则需要调用它。

Try this Fiddle which is a modification of your original code 试试这个小提琴 ,它是对原始代码的修改

You've got a few minor mistakes--mostly the declration of the var gencolor inside the selectColor function, after declaring it globally. 您有一些小错误-全局声明之后,大多数是selectColor函数中var gencolor的偏斜。

This works fine (see it in this fiddle ): 可以正常工作(请在小提琴中查看):

//global var
var gencolor;

$(document).ready(function() {
     load_test_map2();
});

function selectColor(){
     var myColors = ["#441540",
     "#20A923",
     "#EF5918",
     "#E33EF3",
     "#C6452B",
     "#F94B34",
     "#236BD4",
     "#51A224",
     "#6E3372"];
     // set the global var, don't redeclare it
     gencolor = myColors[Math.round(Math.random() * (myColors.length - 1))];
}

function load_test_map2() {
    selectColor();
    alert(gencolor); 
}

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

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