Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 每X秒加载js文件并更新内容_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 每X秒加载js文件并更新内容

Javascript 每X秒加载js文件并更新内容,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有一段JS代码,在前端显示数据和数组。看起来像这样: var compliments = { complimentLocation: '.compliment', currentCompliment: '', complimentList: { 'morning': mycompliments.morning, 'afternoon': mycompliments.afternoon, 'evening': mycompl

我有一段JS代码,在前端显示数据和数组。看起来像这样:

var compliments = {
    complimentLocation: '.compliment',
    currentCompliment: '',
    complimentList: {
        'morning': mycompliments.morning,
        'afternoon': mycompliments.afternoon,
        'evening': mycompliments.evening
    },
    updateInterval: mycompliments.interval || 30000,
    fadeInterval: mycompliments.fadeInterval || 4000,
    intervalId: null
};

compliments.updateCompliment = function () {

    var _list = [];

    var hour = moment().hour();

    if (hour >= 3 && hour < 12) {
        _list = compliments.complimentList['morning'].slice();
    } else if (hour >= 12 && hour < 17) {
        _list = compliments.complimentList['afternoon'].slice();
    } else if (hour >= 17 || hour < 3) {
        _list = compliments.complimentList['evening'].slice();
    } else {
        Object.keys(compliments.complimentList).forEach(function (_curr) {
            _list = _list.concat(compliments.complimentList[_curr]).slice();
        });
    }

    var _spliceIndex = _list.indexOf(compliments.currentCompliment);
    if (_spliceIndex !== -1) {
        _list.splice(_spliceIndex, 1);
    }

    var _randomIndex = Math.floor(Math.random() * _list.length);
    compliments.currentCompliment = _list[_randomIndex];

    $('.compliment').updateWithText(compliments.currentCompliment, compliments.fadeInterval);

}

compliments.init = function () {

    this.updateCompliment();

    this.intervalId = setInterval(function () {
        this.updateCompliment();
    }.bind(this), this.updateInterval)

}

我遗漏了什么?

在这种情况下,setInterval不是更有用吗?e、 g.设置间隔(functionToLoadFile,1000);然后将它移出函数toloadfile,并在页面加载时启动它。函数toloadfile是否多次设置了compaids.init,而实际上没有自己做任何事情。。。您对从“mycompliments.js”中获取的数据不做任何处理,只是(重新)定义了“complaid”对象的“init”函数(该函数还有一个区间函数,我认为如果您在“functionToLoadFile”函数中调用它将不是一个好主意),并且您正在每秒调用它。也许你应该用'data'作为参数调用updateCompliment。。。
function functionToLoadFile(){
  jQuery.get('js/mycompliments.js', function(data) {

      compliments.init = function () {

        this.updateCompliment();

        this.intervalId = setInterval(function () {
            this.updateCompliment();
        }.bind(this), this.updateInterval)

      }

   setTimeout(functionToLoadFile, 1000);
});
}