全局变量在函数内部更新,但即使使用函数执行也不会更新

时间:2018-01-25 19:37:48

标签: javascript

我目前正在制作游戏的工作菜单。我在代码的顶部声明了一个全局变量,我想从函数中更新它。我希望它在函数中被更改后全局更新,我相信它没有做任何额外的事情(据我所知,习惯于c ++,它完全不同)。

但是,我得到了第二个名为init()的函数,该函数在其他所有函数之前运行。有没有办法可以检索window.addEventListener中正在更新的playerJob,并且这样做我可以在init()函数中使用它吗?因为它在window.addEventListener之前运行?

我试着简单地将init()放在window.addEventListener下面,但这没什么用。

这是完整的代码注释(其中一些是不相关的,为其中一些添加了“NOT RELEVANT”: https://pastebin.com/1i6AjeMX

var playerJob = ""; // global playerJob varaible that i'd like to update in the function below

$( function() {
// This is the second function
// Need it to access the window.addEventListener first to retrieve the player's job
// This because, as you can see in the init function, it needs to know the value of playerJob
// in order to show the right data-require.
    init();

// Gets the actionmenu div container        - NOT RELEVANT HERE
var actionContainer = $( "#actionmenu" );

// Listens for NUI messages from Lua 
// This needs to be accessed before init() as it has the playerJob data in it.
window.addEventListener( 'message', function( event ) {
    var item = event.data;


    // Show the menu 
    if ( item.showmenu ) {
        ResetMenu()
        actionContainer.show();
    }

    // Hide the menu 
    if ( item.hidemenu ) {
        actionContainer.hide(); 
    }


    if ( item.updateJob ) {
        // update the job variable 

        console.log("before set playerJob")
        playerJob = item.job; 
        console.log("playerJob set to: ")
        console.log(playerJob)

        // Obviously the console log returns the correct value of the playerJob, however
        // it doesn't seem to update the global variable shown in the start of the code
    }

    console.log("after the whole item updatejob")
    console.log(playerJob)
    // Here it doesn't show the value that was updated in the function above, however
    // it returns the value of the playerJob that I assigend globally, which is = "" - empty
    // My goal is to update the playerJob in the window.addEventListener and also update the global variable
    // at the same time

} );
} )

console.log("this is the job outside after update: ")
console.log(playerJob)
// this doens't work. Still empty, hasn't been updated from the 
window.addEventListener - returning the global = "" one

// Hides all div elements that contain a data-parent, in            -- NOT 
RELEVANT HERE
// other words, hide all buttons in submenus. 
function ResetMenu() {
$( "div" ).each( function( i, obj ) {
    var element = $( this );

    if ( element.attr( "data-parent" ) ) {
        element.hide();
    } else {
        element.show();
    }
} );
}

// Configures every button click to use its data-action, or data-sub                        
-- NOT RELEVANT HERE
// to open a submenu. 


function init() {
// Loops through every button that has the class of "menuoption"
$( ".menuoption" ).each( function( i, obj ) {

    // If the button has a data-action, then we set it up so when it is 
    // pressed, we send the data to the lua side. 
    if ( $( this ).attr( "data-action" ) ) {
        $( this ).click( function() { 
            var data = $( this ).data( "action" ); 

            sendData( "ButtonClick", data ); 
        } )
    }

    // If the button has a data-sub, then we set it up so when it is 
    // pressed, we show the submenu buttons, and hide all of the others.
    if ( $( this ).attr( "data-sub" ) ) {
        $( this ).click( function() {
            var menu = $( this ).data( "sub" );
            var element = $( "#" + menu ); 
            element.show();
            $( this ).parent().hide();  
        } )
    }

    // wk_actionmenu        THIS is where I'd love to access the playerJob     
updated in the window.addEventListener
    if ( ( $( this ).attr ( "data-require" ) == "police" && playerJob != 
"police") ) {
        $( this ).hide();
    }
} );

}

// Send data to lua for processing.             -- NOT RELEVANT HERE
function sendData( name, data ) {
$.post( "http://wk_actionmenu/" + name, JSON.stringify( data ), function( datab ) {
    if ( datab != "ok" ) {
        console.log( datab );
    }            
} );
}

1 个答案:

答案 0 :(得分:0)

你所拥有的部分

console.log("this is the job outside after update: ")
console.log(playerJob)
事件监听器触发后不会发生

。在注册事件监听器之后,您刚刚放置了这些日志语句:

window.addEventListener( 'message', function( event ) { ...

这部分应该可以正常工作(注意log方法可以使用任意数量的args):

if ( item.updateJob ) {
    // update the job variable 

    console.log("before set playerJob")
    playerJob = item.job; 
    console.log("playerJob set to: ", playerJob)
}

console.log("after the whole item updatejob", playerJob);
知道消息之后

事件监听器已经触发,您应该在控制台中看到这些日志(如果item.updateJob是真的),那么您可以在控制台中键入playerJob并且应该更新