Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.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 如何阻止js运行?_Javascript_Jquery_Html_Ajax - Fatal编程技术网

Javascript 如何阻止js运行?

Javascript 如何阻止js运行?,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,我在YouTube上看过一个视频教程,当用户到达页面底部时,它会自动加载新的div(类似于facebook主页)。而不是预期的结果,它只是继续加载。我不知道该怎么阻止。以下是完整的代码: <!DOCTYPE html> <html> <head> <script> function yHandler(){ // Watch video for line by line explanation of the code // h

我在YouTube上看过一个视频教程,当用户到达页面底部时,它会自动加载新的div(类似于facebook主页)。而不是预期的结果,它只是继续加载。我不知道该怎么阻止。以下是完整的代码:

    <!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>

函数yHandler(){
//观看视频,逐行解释代码
// http://www.youtube.com/watch?v=eziREnZPml4
var wrap=document.getElementById('wrap');
var contentHeight=wrap.offsetHeight;
var yOffset=window.pageYOffset;
变量y=yOffset+window.innerHeight;
如果(y>=内容高度){
//这里是获取更多动态数据的Ajax调用
wrap.innerHTML+='';
}
var status=document.getElementById('status');
status.innerHTML=contentHeight+“|”+y;
}
window.onscroll=yHandler;
div#status{位置:固定;字体大小:24px;}
div#wrap{宽度:800px;边距:0px自动;}
div.newData{高度:1000px;背景:09F;边距:10px 0px;}
0 | 0

要在桌面上测试它,请确保您拥有名为temp9的图像。jpg

您需要添加一个检查以查看是否正在提取内容,如果是,则忽略代码,直到加载数据

var isLoading = false;
function yHandler(){
    if (isLoading) {
       return;
    }
    /* Rest of logic */
}
现在,当Ajax调用完成后,将
isLoading
设置回
false

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