简体   繁体   English

在 Javascript 中更改对象的属性

[英]Changing property of an object in Javascript

Hi i am trying to refresh the value of a property inside an object after delay of one second i am using setInterval function for that purpose but value of the property i get is not right嗨,我正在尝试在延迟一秒后刷新对象内属性的值,为此我正在使用 setInterval 函数,但我得到的属性值不正确

            var nextlvl = '';
            function showPlaybackData() {

                            if(j.nextLevel == -1){

                                nextlvl = '';

                            }else if(j.nextLevel == 0){

                                nextlvl = '240p';

                            }else if(j.nextLevel == 1){

                                nextlvl = '360p';

                            }else if(j.nextLevel == 2){

                                nextlvl = '480p';

                            }else if(j.nextLevel == 3){

                                nextlvl = '720p';

                            }else if(j.nextLevel == 4){

                                nextlvl = '1080p';

                            }
                            console.log(nextlvl);
                            return nextlvl;

                        }

                var g = {
                    id: -1,
                    label: 'auto(' + setInterval( showPlaybackData , 1000) +')',
                    selected: -1 === j.manualLevel
                };
                e.push(g)

the property label's value should be one of these resolutions i am setting according to condition but it shows 14 or 18 or 15. any solution to that?属性标签的值应该是我根据条件设置的这些分辨率之一,但它显示 14 或 18 或 15。有什么解决方案吗? when i use console.log i get accurate value within the function but when i call the function in setInterval the value is not right.当我使用console.log时,我在函数中获得了准确的值,但是当我在setInterval调用该函数时,该值不正确。

You need to refactor the code so that you use nextlvl within the function, instead of returning it.您需要重构代码,以便在函数中使用nextlvl ,而不是返回它。 It's difficult to answer without seeing your full code, but something like this :如果没有看到完整的代码,很难回答,但是像这样:

var j = ....
var e = [];
var g = {
    id: -1,
    selected: -1 === j.manualLevel
};
function showPlaybackData() {
    var nextlvl = '';
    if(j.nextLevel == 8){
        nextlvl = '';
    }else if(j.nextLevel == 0){
        nextlvl = '240p';
    }else if(j.nextLevel == 1){
        nextlvl = '360p';
    }else if(j.nextLevel == 2){
        nextlvl = '480p';
    }else if(j.nextLevel == 3){
        nextlvl = '720p';
    }else if(j.nextLevel == 4){
        nextlvl = '1080p';
    }
    console.log(nextlvl);
    g.label = 'auto(' + nextlvl +')';
    e.push(g);
};

setInterval(showPlaybackData, 1000);

As a note, you should consider looking at switch statements, if you're not aware of them, as an alternative to the if/else s.请注意,您应该考虑查看switch语句,如果您不知道它们,作为if/else的替代。

setInterval returns an identifier that allows you to retrieve (and, for example, clear) the timer, rather than the result of the function. setInterval 返回一个标识符,允许您检索(例如,清除)计时器,而不是函数的结果。 Rather than returning the value you want in the function that's looped, you'll need to set a value in an object whose scope you have access to from within that loop.您不需要在循环的函数中返回您想要的值,而是需要在一个对象中设置一个值,您可以从该循环内访问该对象的范围。

May be this works for you :可能这对你有用:

function showPlaybackData(jpassed) {                    
                        var nextlvl = '';
                        switch(jpassed.nextLevel) {
                            case '':
                                      nextlvl = '';
                                      break;
                            case '240p':
                                      nextlvl = '240p';
                                      break;
                            case '360p':
                                      nextlvl = '360p';
                                      break;
                            case '480p':
                                      nextlvl = '480p';
                                      break;
                            case '720p':
                                      nextlvl = '720p';
                                      break;
                            case '1080p':
                                      nextlvl = '1080p';
                                      break;
                            default:
                                // any default logic
                        }
                        console.log(nextlvl);
                        return nextlvl;
}

And then update your object inside setinterval :然后在 setinterval 中更新您的对象:

var j = ...
var g = {
             id: -1,
             label:'',
             selected: -1 === j.manualLevel
         };
setInterval(function(){
                          g.label = 'auto(' +showPlaybackData(j)+')' ;
                          e.push(g)                              
                    },2000,j,showPlaybackData,g);

@Karl Reid : Thanx Karl, I've updated the answer. @Karl Reid:Thanx Karl,我已经更新了答案。

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

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