Javascript 带有睡眠/超时的jQuery请求循环

Javascript 带有睡眠/超时的jQuery请求循环,javascript,jquery,timeout,settimeout,Javascript,Jquery,Timeout,Settimeout,我一直在尝试创建一个jQuery请求循环,根据结果每隔X秒重新加载一次内容 我有这个JS: $(document).ready(function(){ function init(){ $.ajax({ url: 'screen.php', data: { 'screen' : <?php echo $screen_id; ?> },

我一直在尝试创建一个jQuery请求循环,根据结果每隔X秒重新加载一次内容

我有这个JS:

$(document).ready(function(){
    function init(){
        $.ajax({
            url: 'screen.php',
            data: {
                'screen' : <?php echo $screen_id; ?>
            },
            async: true,
            success: function(r){
                JSON.parse(r, function(k, v){
                    if(k == 'screen_content'){
                        var content = v;/* $('.content').html(v); */
                    }

                    if(k == 'visible_seconds'){
                        setTimeout($('.content').html(content),v);
                        /* (function(){}).delay(timer); */
                        /* $().delay(function(msg) { console.log(msg); }, v, 'Hello'); */
                    }
                });

                /* init(); */
            }
        });
    }

    init();
});
$(文档).ready(函数(){
函数init(){
$.ajax({
url:'screen.php',
数据:{
“屏幕”:
},
async:true,
成功:功能(r){
parse(r,函数(k,v){
如果(k==‘屏幕内容’){
var content=v;/*$('.content').html(v)*/
}
如果(k=‘可见秒’){
setTimeout($('.content').html(content),v);
/*(函数(){}).delay(计时器)*/
/*$().delay(函数(msg){console.log(msg);},v,'Hello')*/
}
});
/*init()*/
}
});
}
init();
});
结果是一个JSON字符串,其中X个“屏幕内容”和“可见秒”成对出现。我需要在“可见秒”内显示“屏幕内容”,然后用JSON中的下一个更改内容-当所有内容都显示出来后,一切都重新开始(这样我们就可以在中获得新内容)


在我看来这很简单,但我无法为它创建jQuery://

您需要这样的东西:

$(document).ready(function() {
    function getContent(){
        $.ajax({
            url: 'screen.php',
            data: {
                'screen' : <?php echo $screen_id; ?>
            },
            async: true,
            success: function(contentArray){
                return showAllContent(contentArray);
            }
        });
    }

    var i = 0;
    function showContent(contentArray, count){
        var currContentData = contentArray[count];
        $('.content').html(currContentData.content);
        setTimeout(function() {
            return showAllContent(contentArray);
        }, currContentData.duration);
    }

    function showAllContent(contentArray){
        if(i === contentArray.length){
            i = 0;
            return getContent(showAllContent);
        }
        return showContent(contentArray, i++);
    }

    getContent();
});
摆弄伪函数而不是ajax调用

[{
    content: 'content 1', 
    duration: 1000
}, {
    content: 'content 2',
    duration: 2000
}]