0

我有这个代码

<script type="text/javascript">
var nextUrlArticle = null;

$(document).ready(function() {
    $.get("{{ path('next_video_url', { 'id' : article.id })}}", function(result) {
        nextUrlArticle = result.url;
    });

    function getNextVideo(ext) {
        if (nextUrlArticle) {
            window.location.href = nextUrlArticle;
        }
    }
});
</script>

而且它永远不会进入“ window.location.href”,这是完全正常的,因为它是用初始化的,null并且有一个 ajax 调用来更新参数,但由于它是异步的,因此在处理代码时它没有效果。

但我真的需要检查 nextUrlArticle 是否不为空,所以我无法删除该检查。 getNextVideo在视频结束时由 Flash 视频播放器调用。(我不知道这部分代码)

如何使该检查通过?

谢谢。

4

3 回答 3

0

AJAX成功后,需要用更新的值调用函数执行;

$(document).ready(function() {
    $.get("{{ path('next_video_url', { 'id' : article.id })}}", function(result) {
        nextUrlArticle = result.url;
        getNextVideo(); // call function after update
    });

    function getNextVideo() {
        if (nextUrlArticle) {
            window.location.href = nextUrlArticle;
        }
    }
});
于 2012-09-10T10:12:16.993 回答
0

好的,我需要将 OUTSIDE 放在getNextVideo()OUTSIDE 中,$(document).ready(function() {因为视频播放器无法调用 .ready 中的函数

很高兴知道。

于 2012-09-10T10:26:44.423 回答
0

我认为视频在 ajax 完成之前调用了该方法。

一种方法是在加载页面时直接传递nexturl,而不是从ajax获取,例如:

nextUrlArticle = {{ nexturl }} //django syntax to pass context vars to template
function getNextVideo() {
    if (nextUrlArticle) {
        window.location.href = nextUrlArticle;
    }
 }
于 2012-09-10T10:21:15.980 回答