hurriedly% 2016-01-14 14:05 采纳率: 100%
浏览 188

如何停止js运行?

I have seen a video tutorial on YouTube about Dynamic Content that when a user reaches the bottom of a page, it automatically loads new div (Something like the facebook homepage). Instead of the intended result, it just keeps on loading. I don't know how to stop that. Here's the full code:

    <!DOCTYPE html>
<html>
<head>
<script>
function yHandler(){
    // Watch video for line by line explanation of the code
    // http://www.youtube.com/watch?v=eziREnZPml4
    var wrap = document.getElementById('wrap');
    var contentHeight = wrap.offsetHeight;
    var yOffset = window.pageYOffset; 
    var y = yOffset + window.innerHeight;
    if(y >= contentHeight){
        // Ajax call to get more dynamic data goes here
        wrap.innerHTML += '<div class="newData"></div>';
    }
    var status = document.getElementById('status');
    status.innerHTML = contentHeight+" | "+y;
}
window.onscroll = yHandler;

</script>
<style>
div#status{position:fixed; font-size:24px;}
div#wrap{width:800px; margin:0px auto;}
div.newData{height:1000px; background:#09F; margin:10px 0px;}
</style>
</head>
<body>
<div id="status">0 | 0</div>
<div id="wrap"><img src="temp9.jpg"></div>
</body>
</html>

To test it on your desktop, make sure you have the image with the name of temp9.jpg

  • 写回答

1条回答 默认 最新

  • weixin_33708432 2016-01-14 16:57
    关注

    You need to add a check to see if the content is being fetched, if it is ignore the code until the data has been loaded.

    var isLoading = false;
    function yHandler(){
        if (isLoading) {
           return;
        }
        /* Rest of logic */
    }
    

    Now when the Ajax call is complete, set isLoading back to false.

    $.get( "example.php", function() {
        alert( "success" );
    }).done(function() {
        isLoading = false;
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么