Javascript jQuery脚本触发两次?可能是现场直播两次?

Javascript jQuery脚本触发两次?可能是现场直播两次?,javascript,jquery,jquery-mobile,Javascript,Jquery,Jquery Mobile,我正在使用最新版本的PhoneGap、jQM和jQuery 1.8.0构建一个应用程序。到目前为止一切都很好,除了我遇到的这个小而烦人的问题 我从包含以下代码的index.html链接了我的show.html: <div id="search"> contains search bar and submit button </div> <div id="list" style="display:none;"> <ul data-role="

我正在使用最新版本的PhoneGap、jQM和jQuery 1.8.0构建一个应用程序。到目前为止一切都很好,除了我遇到的这个小而烦人的问题

我从包含以下代码的index.html链接了我的show.html:

<div id="search"> contains search bar and submit button </div>
<div id="list" style="display:none;"> 
      <ul data-role="listview" data-filter="true"> </ul>
</div>
这会向firebug控制台打印两次“页面结尾”!此脚本存在于head标记之间

有没有办法只打印一次?我之所以需要它,是因为我实现了自己的无止境滚动功能,而问题是导致我的ajax请求两次发送到服务器。该函数工作得很好,但不是问题的原因,因为即使我将lastPostFunc()注释掉,它仍然会打印到控制台两次

编辑:就个人而言,我不认为目前给出的答案是错误的,它们是正确的,但它们不能帮助我解决问题。也许我需要更好地重新措辞。我复制了我的代码并将其粘贴到一个独立的页面上,它只打印一次,所以我的代码实际上没有任何问题,它的工作方式应该是这样的

因此,我想知道是否有其他原因导致它在提交表单后打印两次。我的表格和我的结果页在同一页上。这是否意味着它导致live事件运行了两次?因此,导致它在pageshow上打印到控制台两次?如果我是正确的,是否有办法解决这个问题,使它只打印一次


感谢您的帮助。谢谢大家!

您的函数实际上会将输出写入控制台两次以上。问题在于您的情况:

if ($(window).scrollTop()+200 >= ($(document).height() - ($(window).height())))  
因为对于该区域内的每个滚动,条件都将计算为true。您可以将其更改为:

if ($(window).scrollTop() == ($(document).height() - ($(window).height())))

希望这能解决你的问题

jQuery(window).scroll(function() {
    if( (jQuery(document).height() < (jQuery(window).scrollTop() + jQuery(window).height() + 200))&&(jQuery(document).height() > (jQuery(window).scrollTop() + jQuery(window).height() + 99))) {        
    console.log('test')                                         
    }
}); 
jQuery(窗口)。滚动(函数(){
if((jQuery(document).height()<(jQuery(window.scrollTop()+jQuery(window.height()+200))&&(jQuery(document.height()>(jQuery(window.scrollTop()+jQuery(window.height()+99)){
console.log('test')
}
}); 
//更新

    var lastlogtime= null;
jQuery(window).scroll(function() {
        if( (jQuery(document).height() < (jQuery(window).scrollTop() + jQuery(window).height() + 200))&&(jQuery(document).height() > (jQuery(window).scrollTop() + jQuery(window).height() + 99))) {
        if(((lastlogtime+600)<(+new Date())) || (lastlogtime== null)){
            //your scroll logic
            console.log('test')         ;
            lastlogtime=    +new Date();                            
            }
        }
    });
var lastlogtime=null;
jQuery(窗口).滚动(函数(){
if((jQuery(document).height()<(jQuery(window.scrollTop()+jQuery(window.height()+200))&&(jQuery(document.height()>(jQuery(window.scrollTop()+jQuery(window.height()+99))){

如果(((lastlogtime+600)好,我终于解决了

显然,我所要做的就是添加这行代码:

    $(window).scroll(function (e) {
        if (reach bottom) {
            e.stopImmediatePropagation();
            console.log('End of the Page');
        } 
    });

它工作了!为我停止了两次打印。

没有。没有工作。没有打印任何东西到控制台。虽然输出不超过两次。它一次连续输出两次。Sry我刚刚在Chrome中尝试过,工作正常。删除+200,这在Firefox中也应该工作。非常抱歉,它不工作。它仍然工作在控制台上打印两次。我在Firefox和Chrome中尝试过。在提交表单之前,我粘贴了一些虚拟文本来填充页面,窗口。滚动启动很好!它打印一次!但是在我提交表单之后,当我向下滚动时,它会一次打印两次。我希望这是有意义的。非常感谢您给我的帮助。尝试将.live应该可以修复它:)我以前尝试过live。但我听说它从1.7.2开始就不受欢迎,所以我改为使用on。这样做仍然会使它打印两次:(感谢您花时间提供帮助,但它不起作用。相反,它根本不会导致打印任何内容。它仍然不会打印任何内容。我不确定您是否看到我对其他用户的其他回复,但如果我使用我的原始代码,它仍然会打印两次到控制台。但在提交表单之前,我粘贴了一些虚拟文本以填充p。)年龄,window.scroll启动正常!它只打印一次!但在我提交后,当我向下滚动时,它会一次打印两次。我希望这是有意义的。是的。它仍然会导致相同的问题:(我已更新了答案,请使用您的代码和单机版进行尝试。让我知道会发生什么情况。提交后仍会打印两次。但我在仅包含虚拟文本的页面上进行了单机版尝试。它打印一次:(这很奇怪。这与我的pageshow事件有关吗?是否因为我提交了表单而加载了两次?但是提交表单只会导致搜索div隐藏。并将结果附加到div。顺便说一句,不要使用.click(),它已被弃用。使用:.on('click',function(){//code here}
    $(window).scroll(function (e) {
        if (reach bottom) {
            e.stopImmediatePropagation();
            console.log('End of the Page');
        } 
    });