简体   繁体   English

函数(从setTimeout调用)不返回值

[英]Function (called from setTimeout) not returning value

I'm learning JavaScript by writing a Chrome extension for a website. 我正在通过为网站编写Chrome扩展程序来学习JavaScript。 The website has a section that displays a list of 4 videos, with a button to load more videos. 该网站有一个部分,显示4个视频的列表,并带有一个按钮以加载更多视频。 When you click this button, it loads a new list of videos via an AJAX call. 当您单击此按钮时,它将通过AJAX调用加载新的视频列表。

I have a function, getNextVideo() , which gets the URL of the next video in the list. 我有一个函数getNextVideo() ,该函数获取列表中下一个视频的URL。 For example, if you are on the third video, then it'll grab the URL for the fourth video. 例如,如果您正在观看第三个视频,则它将获取第四个视频的URL。 If you're on the fourth video, then it'll simulate a click (which loads the new list of videos) via a function getNextVideoList() and then grab the first in the newly loaded list after a 1s timeout to wait for the DOM to update (I tried to use mutation observers but it was too complicated for my skill level). 如果您正在观看第四个视频,它将通过函数getNextVideoList()模拟一次点击(加载新的视频列表getNextVideoList() ,然后在等待1秒的超时后在新加载的列表中获取第一个视频,以等待DOM更新(我尝试使用突变观测器,但是对于我的技能水平来说太复杂了)。

function getNextVideo()
{
    if (getVideoPosition() == 4)
    {

        function ad()
        {
            getVideo(1);
        }

        setTimeout(ad, 1000);

        getNextVideoList();
    }
    else
    {
        var index = getVideoPosition() + 1;
        return getVideo(index);
    }
}

getVideoPosition() gets the index of the video in the list using some simple jQuery nth-child selectors. getVideoPosition()使用一些简单的jQuery nth-child选择器获取列表中视频的索引。 It returns the values 1 , 2 , 3 , or 4 . 它返回值123 ,或4

getVideo(n) takes the index of the video (1, 2, 3, or 4) and returns the URL of that video. getVideo(n)获取视频的索引(1、2、3或4)并返回该视频的URL。 The returned value looks like this in the console: "http://video.example.com/video-id" 在控制台中,返回的值如下所示: "http://video.example.com/video-id"

My problem is that when I run getNextVideo() on a video that is 4th in the queue, I get undefined returned in my console. 我的问题是,当我在队列中第4个视频上运行getNextVideo()时,在控制台中返回undefined返回值。 How do I make the getNextVideo() function return the URL of the first video in the list after the AJAX call and subsequent DOM update? 在AJAX调用和后续DOM更新之后,如何使getNextVideo()函数返回列表中第一个视频的URL?

This is because getNextVideo() doesn't have a return value when getVideoPosition() == 4 . 这是因为当getVideoPosition() == 4时, getNextVideo()没有返回值。 No return value shows up as undefined . 没有返回值显示为undefined

If you look at your code, there's no return inside the if (getVideoPosition() == 4) block. 如果您看一下代码,则if (getVideoPosition() == 4)块内没有任何返回值。 When there's no return from a function, the return value is undefined . 当函数没有返回值时,返回值是undefined If you want there to be a return value, then you have to add a return statement. 如果要有一个返回值,则必须添加一个return语句。 Note, the function called by setTimeout() gets executed AFTER getNextVideo() has already returned so it can't participate in setting the return value. 请注意,由setTimeout()调用的函数将在getNextVideo()返回之后执行,因此它不能参与设置返回值。

If you really want to use asynchronous programming with setTimeout() (I'm not sure why you're doing that in the first place), then you will have to switch to an asynchronous programming model which means you won't use a return value from getNextVideo() at all, but will call a callback when it's done with the next value. 如果您真的想通过setTimeout()使用异步编程(我不确定为什么要这么做),那么您将不得不切换到异步编程模型,这意味着您将不会使用return完全来自getNextVideo()值,但是在使用下一个值完成操作时将调用回调。 That callback can be executed asynchronously from within the setTimeout() . 可以从setTimeout()内部异步执行该回调。 But, I don't know why you're even using setTimeout() in the first place so I don't know if that extra complication is the best choice or not. 但是,我不知道为什么首先还要使用setTimeout() ,所以我不知道额外的复杂性是否是最佳选择。 If you got rid of the asynchronous setTimeout() then you could just return a value normally from that arm of getNextVideo() too. 如果您摆脱了异步setTimeout()那么通常也可以从getNextVideo()getNextVideo()返回一个值。

As far as my understanding to your code you are transferring control from your function "getNextVideo" to setTimeOut callback when condition occur. 据我对代码的理解,当条件发生时,您正在将控制权从函数“ getNextVideo”转移到setTimeOut回调。
Actual duty of your "getNextVideo" function is to "return getVideo(index);" 您的“ getNextVideo”函数的实际职责是“返回getVideo(index);”。

is it possible if you do your code like this. 如果这样做,是否有可能

        if (getVideoPosition() == 4)
{
    function ad()
    {
        getNextVideo(true);
    }
    setTimeout(ad, 1000);
    getNextVideoList();
}


function getNextVideo(isFirst)
{
    var index = isFirst==true?1:getVideoPosition() + 1;
    return getVideo(index);
}

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

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