Javascript 如何利用Ajax动态更新基础轨道滑块

Javascript 如何利用Ajax动态更新基础轨道滑块,javascript,ajax,orbit,Javascript,Ajax,Orbit,我使用foundation4 orbit插件来显示图像库 <script type="text/javascript" charset="utf-8"> Array.prototype.contains = function(obj) { var i = this.length; while (i--) { if (this[i] == obj) { return true; } } retur

我使用foundation4 orbit插件来显示图像库

<script type="text/javascript" charset="utf-8"> 
Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] == obj) {
            return true;
        }
    }
    return false;
}

function getParameterByName(name){
      name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
      var regexS = "[\\?&]" + name + "=([^&#]*)";
      var regex = new RegExp(regexS);
      var results = regex.exec(window.location.search);
      if(results == null)
        return "";
      else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

var images = [];

function addmsg(type, msg){
    var updated = false;
    for (var i in msg){     
        if (!images.contains(msg[i])){
            $("#messages").prepend(
                "<li><img src='"+msg[i]+"'"+"/>"+"<div class='orbit-caption'>...</div>"
            );
            images.push(msg[i]);
            updated = true;
        }
    }
    return updated;
}

var url = 'get_latest_images.php';

function waitForMsg(){
    /* This requests the url "msgsrv.php"
    When it complete (or errors)*/
    $.ajax({
        type: "GET",
        url: url+'?username='+getParameterByName('username'),
        dataType: 'json',
        async: true, /* If set to non-async, browser shows page as "Loading.."*/
        cache: false,
        timeout:50000, /* Timeout in ms */

        success: function(data){ /* called when request to barge.php completes */
            //alert(data);
            var updated = addmsg("new", data); /* Add response to a .msg div (with the "new" class)*/
            if (updated){
                $(document).foundation();
            }
            setTimeout(
                waitForMsg, /* Request next message */
                1000 /* ..after 1 seconds */
            );
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            addmsg("error", textStatus + " (" + errorThrown + ")");
            setTimeout(
                waitForMsg, /* Try again after.. */
                15000); /* milliseconds (15seconds) */
        }
    });
};

$(document).ready(function(){
    waitForMsg(); /* Start the inital request */            
});
</script>
<script src="./js/foundation.min.js"></script>
<script src="./js/foundation.orbit.js"></script>
它目前的工作是:

<script type="text/javascript" charset="utf-8"> 
Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] == obj) {
            return true;
        }
    }
    return false;
}

function getParameterByName(name){
      name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
      var regexS = "[\\?&]" + name + "=([^&#]*)";
      var regex = new RegExp(regexS);
      var results = regex.exec(window.location.search);
      if(results == null)
        return "";
      else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

var images = [];

function addmsg(type, msg){
    var updated = false;
    for (var i in msg){     
        if (!images.contains(msg[i])){
            $("#messages").prepend(
                "<li><img src='"+msg[i]+"'"+"/>"+"<div class='orbit-caption'>...</div>"
            );
            images.push(msg[i]);
            updated = true;
        }
    }
    return updated;
}

var url = 'get_latest_images.php';

function waitForMsg(){
    /* This requests the url "msgsrv.php"
    When it complete (or errors)*/
    $.ajax({
        type: "GET",
        url: url+'?username='+getParameterByName('username'),
        dataType: 'json',
        async: true, /* If set to non-async, browser shows page as "Loading.."*/
        cache: false,
        timeout:50000, /* Timeout in ms */

        success: function(data){ /* called when request to barge.php completes */
            //alert(data);
            var updated = addmsg("new", data); /* Add response to a .msg div (with the "new" class)*/
            if (updated){
                $(document).foundation();
            }
            setTimeout(
                waitForMsg, /* Request next message */
                1000 /* ..after 1 seconds */
            );
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            addmsg("error", textStatus + " (" + errorThrown + ")");
            setTimeout(
                waitForMsg, /* Try again after.. */
                15000); /* milliseconds (15seconds) */
        }
    });
};

$(document).ready(function(){
    waitForMsg(); /* Start the inital request */            
});
</script>
<script src="./js/foundation.min.js"></script>
<script src="./js/foundation.orbit.js"></script>
(1) 使用AJAX动态加载图像列表,AJAX调用每隔1秒定期轮询一次

<script type="text/javascript" charset="utf-8"> 
Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] == obj) {
            return true;
        }
    }
    return false;
}

function getParameterByName(name){
      name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
      var regexS = "[\\?&]" + name + "=([^&#]*)";
      var regex = new RegExp(regexS);
      var results = regex.exec(window.location.search);
      if(results == null)
        return "";
      else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

var images = [];

function addmsg(type, msg){
    var updated = false;
    for (var i in msg){     
        if (!images.contains(msg[i])){
            $("#messages").prepend(
                "<li><img src='"+msg[i]+"'"+"/>"+"<div class='orbit-caption'>...</div>"
            );
            images.push(msg[i]);
            updated = true;
        }
    }
    return updated;
}

var url = 'get_latest_images.php';

function waitForMsg(){
    /* This requests the url "msgsrv.php"
    When it complete (or errors)*/
    $.ajax({
        type: "GET",
        url: url+'?username='+getParameterByName('username'),
        dataType: 'json',
        async: true, /* If set to non-async, browser shows page as "Loading.."*/
        cache: false,
        timeout:50000, /* Timeout in ms */

        success: function(data){ /* called when request to barge.php completes */
            //alert(data);
            var updated = addmsg("new", data); /* Add response to a .msg div (with the "new" class)*/
            if (updated){
                $(document).foundation();
            }
            setTimeout(
                waitForMsg, /* Request next message */
                1000 /* ..after 1 seconds */
            );
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            addmsg("error", textStatus + " (" + errorThrown + ")");
            setTimeout(
                waitForMsg, /* Try again after.. */
                15000); /* milliseconds (15seconds) */
        }
    });
};

$(document).ready(function(){
    waitForMsg(); /* Start the inital request */            
});
</script>
<script src="./js/foundation.min.js"></script>
<script src="./js/foundation.orbit.js"></script>
(2) 通过$(document).foundation()在AJAX成功回调中使用上述结果更新滑块,这将正确显示动态观察滑块

<script type="text/javascript" charset="utf-8"> 
Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] == obj) {
            return true;
        }
    }
    return false;
}

function getParameterByName(name){
      name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
      var regexS = "[\\?&]" + name + "=([^&#]*)";
      var regex = new RegExp(regexS);
      var results = regex.exec(window.location.search);
      if(results == null)
        return "";
      else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

var images = [];

function addmsg(type, msg){
    var updated = false;
    for (var i in msg){     
        if (!images.contains(msg[i])){
            $("#messages").prepend(
                "<li><img src='"+msg[i]+"'"+"/>"+"<div class='orbit-caption'>...</div>"
            );
            images.push(msg[i]);
            updated = true;
        }
    }
    return updated;
}

var url = 'get_latest_images.php';

function waitForMsg(){
    /* This requests the url "msgsrv.php"
    When it complete (or errors)*/
    $.ajax({
        type: "GET",
        url: url+'?username='+getParameterByName('username'),
        dataType: 'json',
        async: true, /* If set to non-async, browser shows page as "Loading.."*/
        cache: false,
        timeout:50000, /* Timeout in ms */

        success: function(data){ /* called when request to barge.php completes */
            //alert(data);
            var updated = addmsg("new", data); /* Add response to a .msg div (with the "new" class)*/
            if (updated){
                $(document).foundation();
            }
            setTimeout(
                waitForMsg, /* Request next message */
                1000 /* ..after 1 seconds */
            );
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            addmsg("error", textStatus + " (" + errorThrown + ")");
            setTimeout(
                waitForMsg, /* Try again after.. */
                15000); /* milliseconds (15seconds) */
        }
    });
};

$(document).ready(function(){
    waitForMsg(); /* Start the inital request */            
});
</script>
<script src="./js/foundation.min.js"></script>
<script src="./js/foundation.orbit.js"></script>
问题在于:

<script type="text/javascript" charset="utf-8"> 
Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] == obj) {
            return true;
        }
    }
    return false;
}

function getParameterByName(name){
      name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
      var regexS = "[\\?&]" + name + "=([^&#]*)";
      var regex = new RegExp(regexS);
      var results = regex.exec(window.location.search);
      if(results == null)
        return "";
      else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

var images = [];

function addmsg(type, msg){
    var updated = false;
    for (var i in msg){     
        if (!images.contains(msg[i])){
            $("#messages").prepend(
                "<li><img src='"+msg[i]+"'"+"/>"+"<div class='orbit-caption'>...</div>"
            );
            images.push(msg[i]);
            updated = true;
        }
    }
    return updated;
}

var url = 'get_latest_images.php';

function waitForMsg(){
    /* This requests the url "msgsrv.php"
    When it complete (or errors)*/
    $.ajax({
        type: "GET",
        url: url+'?username='+getParameterByName('username'),
        dataType: 'json',
        async: true, /* If set to non-async, browser shows page as "Loading.."*/
        cache: false,
        timeout:50000, /* Timeout in ms */

        success: function(data){ /* called when request to barge.php completes */
            //alert(data);
            var updated = addmsg("new", data); /* Add response to a .msg div (with the "new" class)*/
            if (updated){
                $(document).foundation();
            }
            setTimeout(
                waitForMsg, /* Request next message */
                1000 /* ..after 1 seconds */
            );
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            addmsg("error", textStatus + " (" + errorThrown + ")");
            setTimeout(
                waitForMsg, /* Try again after.. */
                15000); /* milliseconds (15seconds) */
        }
    });
};

$(document).ready(function(){
    waitForMsg(); /* Start the inital request */            
});
</script>
<script src="./js/foundation.min.js"></script>
<script src="./js/foundation.orbit.js"></script>
AJAX回调将多次调用$(document).foundation(),每次都会创建一个新的滑块并与现有滑块重叠。如何更新当前的“动态观察”滑块,而不是创建一个新滑块

<script type="text/javascript" charset="utf-8"> 
Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] == obj) {
            return true;
        }
    }
    return false;
}

function getParameterByName(name){
      name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
      var regexS = "[\\?&]" + name + "=([^&#]*)";
      var regex = new RegExp(regexS);
      var results = regex.exec(window.location.search);
      if(results == null)
        return "";
      else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

var images = [];

function addmsg(type, msg){
    var updated = false;
    for (var i in msg){     
        if (!images.contains(msg[i])){
            $("#messages").prepend(
                "<li><img src='"+msg[i]+"'"+"/>"+"<div class='orbit-caption'>...</div>"
            );
            images.push(msg[i]);
            updated = true;
        }
    }
    return updated;
}

var url = 'get_latest_images.php';

function waitForMsg(){
    /* This requests the url "msgsrv.php"
    When it complete (or errors)*/
    $.ajax({
        type: "GET",
        url: url+'?username='+getParameterByName('username'),
        dataType: 'json',
        async: true, /* If set to non-async, browser shows page as "Loading.."*/
        cache: false,
        timeout:50000, /* Timeout in ms */

        success: function(data){ /* called when request to barge.php completes */
            //alert(data);
            var updated = addmsg("new", data); /* Add response to a .msg div (with the "new" class)*/
            if (updated){
                $(document).foundation();
            }
            setTimeout(
                waitForMsg, /* Request next message */
                1000 /* ..after 1 seconds */
            );
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            addmsg("error", textStatus + " (" + errorThrown + ")");
            setTimeout(
                waitForMsg, /* Try again after.. */
                15000); /* milliseconds (15seconds) */
        }
    });
};

$(document).ready(function(){
    waitForMsg(); /* Start the inital request */            
});
</script>
<script src="./js/foundation.min.js"></script>
<script src="./js/foundation.orbit.js"></script>
试验

<script type="text/javascript" charset="utf-8"> 
Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] == obj) {
            return true;
        }
    }
    return false;
}

function getParameterByName(name){
      name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
      var regexS = "[\\?&]" + name + "=([^&#]*)";
      var regex = new RegExp(regexS);
      var results = regex.exec(window.location.search);
      if(results == null)
        return "";
      else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

var images = [];

function addmsg(type, msg){
    var updated = false;
    for (var i in msg){     
        if (!images.contains(msg[i])){
            $("#messages").prepend(
                "<li><img src='"+msg[i]+"'"+"/>"+"<div class='orbit-caption'>...</div>"
            );
            images.push(msg[i]);
            updated = true;
        }
    }
    return updated;
}

var url = 'get_latest_images.php';

function waitForMsg(){
    /* This requests the url "msgsrv.php"
    When it complete (or errors)*/
    $.ajax({
        type: "GET",
        url: url+'?username='+getParameterByName('username'),
        dataType: 'json',
        async: true, /* If set to non-async, browser shows page as "Loading.."*/
        cache: false,
        timeout:50000, /* Timeout in ms */

        success: function(data){ /* called when request to barge.php completes */
            //alert(data);
            var updated = addmsg("new", data); /* Add response to a .msg div (with the "new" class)*/
            if (updated){
                $(document).foundation();
            }
            setTimeout(
                waitForMsg, /* Request next message */
                1000 /* ..after 1 seconds */
            );
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            addmsg("error", textStatus + " (" + errorThrown + ")");
            setTimeout(
                waitForMsg, /* Try again after.. */
                15000); /* milliseconds (15seconds) */
        }
    });
};

$(document).ready(function(){
    waitForMsg(); /* Start the inital request */            
});
</script>
<script src="./js/foundation.min.js"></script>
<script src="./js/foundation.orbit.js"></script>

Array.prototype.contains=函数(obj){
var i=该长度;
而(我--){
如果(此[i]==obj){
返回true;
}
}
返回false;
}
函数getParameterByName(名称){
名称=名称。替换(/[\[]/,“\\\[”)。替换(/[\]]/,“\\\]”);
var regexS=“[\\?&]”+name+“=([^&\\]*)”;
var regex=新的RegExp(regexS);
var results=regex.exec(window.location.search);
如果(结果==null)
返回“”;
其他的
返回组件(结果[1]。替换(/\+/g,”);
}
var图像=[];
函数addmsg(类型,msg){
var=false;
对于(msg中的变量i){
如果(!images.contains(msg[i])){
$(“#消息”)。预结束(
“
  • ”+“…” ); images.push(msg[i]); 更新=真; } } 更新的回报; } var url='get_latest_images.php'; 函数waitForMsg(){ /*这将请求url“msgsrv.php” 完成时(或错误)*/ $.ajax({ 键入:“获取”, url:url+'?用户名='+getParameterByName('username'), 数据类型:“json”, async:true,/*如果设置为非异步,浏览器将页面显示为“加载…”*/ cache:false, 超时:50000,/*以毫秒为单位的超时*/ 成功:对barge.php的请求完成时调用函数(数据){/**/ //警报(数据); var updated=addmsg(“new”,data);/*将响应添加到.msg div(使用“new”类)*/ 如果(更新){ $(document.foundation(); } 设置超时( waitForMsg,/*请求下一条消息*/ 1000/*…1秒后*/ ); }, 错误:函数(XMLHttpRequest、textStatus、errorshown){ addmsg(“错误”,textStatus+”(“+errorSprown+”)); 设置超时( waitForMsg,/*请在..之后重试*/ 15000);/*毫秒(15秒)*/ } }); }; $(文档).ready(函数(){ waitForMsg();/*启动初始请求*/ });

  • <script type="text/javascript" charset="utf-8"> 
    Array.prototype.contains = function(obj) {
        var i = this.length;
        while (i--) {
            if (this[i] == obj) {
                return true;
            }
        }
        return false;
    }
    
    function getParameterByName(name){
          name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
          var regexS = "[\\?&]" + name + "=([^&#]*)";
          var regex = new RegExp(regexS);
          var results = regex.exec(window.location.search);
          if(results == null)
            return "";
          else
            return decodeURIComponent(results[1].replace(/\+/g, " "));
    }
    
    var images = [];
    
    function addmsg(type, msg){
        var updated = false;
        for (var i in msg){     
            if (!images.contains(msg[i])){
                $("#messages").prepend(
                    "<li><img src='"+msg[i]+"'"+"/>"+"<div class='orbit-caption'>...</div>"
                );
                images.push(msg[i]);
                updated = true;
            }
        }
        return updated;
    }
    
    var url = 'get_latest_images.php';
    
    function waitForMsg(){
        /* This requests the url "msgsrv.php"
        When it complete (or errors)*/
        $.ajax({
            type: "GET",
            url: url+'?username='+getParameterByName('username'),
            dataType: 'json',
            async: true, /* If set to non-async, browser shows page as "Loading.."*/
            cache: false,
            timeout:50000, /* Timeout in ms */
    
            success: function(data){ /* called when request to barge.php completes */
                //alert(data);
                var updated = addmsg("new", data); /* Add response to a .msg div (with the "new" class)*/
                if (updated){
                    $(document).foundation();
                }
                setTimeout(
                    waitForMsg, /* Request next message */
                    1000 /* ..after 1 seconds */
                );
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                addmsg("error", textStatus + " (" + errorThrown + ")");
                setTimeout(
                    waitForMsg, /* Try again after.. */
                    15000); /* milliseconds (15seconds) */
            }
        });
    };
    
    $(document).ready(function(){
        waitForMsg(); /* Start the inital request */            
    });
    </script>
    
    <script src="./js/foundation.min.js"></script>
    <script src="./js/foundation.orbit.js"></script>
    
    
    

    <script type="text/javascript" charset="utf-8"> 
    Array.prototype.contains = function(obj) {
        var i = this.length;
        while (i--) {
            if (this[i] == obj) {
                return true;
            }
        }
        return false;
    }
    
    function getParameterByName(name){
          name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
          var regexS = "[\\?&]" + name + "=([^&#]*)";
          var regex = new RegExp(regexS);
          var results = regex.exec(window.location.search);
          if(results == null)
            return "";
          else
            return decodeURIComponent(results[1].replace(/\+/g, " "));
    }
    
    var images = [];
    
    function addmsg(type, msg){
        var updated = false;
        for (var i in msg){     
            if (!images.contains(msg[i])){
                $("#messages").prepend(
                    "<li><img src='"+msg[i]+"'"+"/>"+"<div class='orbit-caption'>...</div>"
                );
                images.push(msg[i]);
                updated = true;
            }
        }
        return updated;
    }
    
    var url = 'get_latest_images.php';
    
    function waitForMsg(){
        /* This requests the url "msgsrv.php"
        When it complete (or errors)*/
        $.ajax({
            type: "GET",
            url: url+'?username='+getParameterByName('username'),
            dataType: 'json',
            async: true, /* If set to non-async, browser shows page as "Loading.."*/
            cache: false,
            timeout:50000, /* Timeout in ms */
    
            success: function(data){ /* called when request to barge.php completes */
                //alert(data);
                var updated = addmsg("new", data); /* Add response to a .msg div (with the "new" class)*/
                if (updated){
                    $(document).foundation();
                }
                setTimeout(
                    waitForMsg, /* Request next message */
                    1000 /* ..after 1 seconds */
                );
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                addmsg("error", textStatus + " (" + errorThrown + ")");
                setTimeout(
                    waitForMsg, /* Try again after.. */
                    15000); /* milliseconds (15seconds) */
            }
        });
    };
    
    $(document).ready(function(){
        waitForMsg(); /* Start the inital request */            
    });
    </script>
    
    <script src="./js/foundation.min.js"></script>
    <script src="./js/foundation.orbit.js"></script>