简体   繁体   English

为什么没有一个接一个地加载javascripts函数?

[英]why javascripts functions are not loaded one after the other?

at the beginning of my html iam initializing variable " movieSRC " like this: 我的html iam初始化变量“ movieSRC ”的开头,如下所示:

<script type="text/javascript">

        var activeStep = getUrlVars();
        var movieSRC;

        cloud.getStepsForModules(activeStep.module, "UMCK").then(function(steps) {

            var calculatedStep = activeStep.step - 1;
            movieSRC = steps.steps[calculatedStep].movie;

        });

at the end of my html i have some function for videojs . 在我的html 末尾 ,我有一些videojs函数。

<script type="text/javascript">
    _V_("movie").ready(function() {
        var myPlayer = this;
        myPlayer.src(movieSRC);
</script>
</html>

I dont understand why a html part at the end will be loaded first and a one after the other at the beginning of my page as a second one. 我不明白为什么在最后一个HTML 部分将被载入第一和one after the other在我的网页开始为第二个。 i need it opposite! 我需要它对面! i need to keep videojs script at the end of the page because with video should be loaded first and just after that a script. 我需要在页面末尾保留videojs脚本,因为应该先加载视频,然后再加载脚本。

conclusion: movieSRC in myPlayer.src(movieSRC) is alwas empty :(, but why? 结论: movieSRC in myPlayer.src(movieSRC)中的movieSRC movieSRC in myPlayer.src(movieSRC)是空的:(,但是为什么呢?

It is executing in order. 它正在按顺序执行。 First a couple of variables are declared, then a function is executed, then later that part at the end executes. 首先声明几个变量,然后执行一个函数,然后执行最后的那一部分。 What you're overlooking is that the function being executed is asynchronous. 您忽略的是正在执行的函数是异步的。 And no matter how fast it runs, it's highly unlikely that it will complete before the page finishes loading. 而且无论它运行的速度有多快,这是极不可能的网页加载完成前,将完成。

This is asynchronous: 这是异步的:

cloud.getStepsForModules(activeStep.module, "UMCK")

So while it's executing everything else is loading. 因此, 当它执行时,其他所有内容都会加载。 Anything that you need to happen after it executes will need to be invoked from its .then() handler: 执行之后需要执行的任何操作都需要从其.then()处理函数中调用:

cloud.getStepsForModules(activeStep.module, "UMCK").then(function(steps) {

    var calculatedStep = activeStep.step - 1;
    movieSRC = steps.steps[calculatedStep].movie;

    // I don't know this plugin, but find your "myPlayer" here somehow
    myPlayer.src(movieSRC);

});

Or perhaps this order will make more sense overall for the plugin: 也许此顺序对插件总体而言更有意义:

_V_("movie").ready(function() {
    var myPlayer = this;

    cloud.getStepsForModules(activeStep.module, "UMCK").then(function(steps) {
        var calculatedStep = activeStep.step - 1;
        movieSRC = steps.steps[calculatedStep].movie;
        myPlayer.src(movieSRC);
    });
});

Most likely, the script at the end is being evaluated before the response returns from the cloud.getStepsForModules function above. 最有可能的是,在响应从上面的cloud.getStepsForModules函数返回之前,对最后的脚本进行评估。

Also, there's a syntax error in the second script - you're missing the closing brace/paren/semicolon set. 另外,第二个脚本中存在语法错误-您缺少右花括号/括号/分号集。

Try putting the function that uses the result in the callback, like 尝试将使用结果的函数放在回调中,例如

<script type="text/javascript">

    var activeStep = getUrlVars();
    var movieSRC;

    cloud.getStepsForModules(activeStep.module, "UMCK").then(function(steps) {

        var calculatedStep = activeStep.step - 1;
        movieSRC = steps.steps[calculatedStep].movie;
        _V_("movie").ready(function() {
           var myPlayer = this;
            myPlayer.src(movieSRC);
            });

    });

JavaScript code does execute in the order it appears. JavaScript代码确实按显示顺序执行。 However, there are functions that are asynchronous, which means they execute in a separate thread. 但是,有些函数是异步的,这意味着它们在单独的线程中执行。 Is it possible that your first function is asynchronous? 您的第一个函数是否可能是异步的?

If you want to make sure that certain code is executed AFTER an asynchronous function, call that code in the asynchronous function's callback method. 如果要确保在异步函数之后执行某些代码,请在异步函数的回调方法中调用该代码。

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

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