Javascript 停止与窗口的承诺

Javascript 停止与窗口的承诺,javascript,jquery,fetch,Javascript,Jquery,Fetch,我使用fetch获取json数据,并用结果呈现列表项。包含fetch的功能由按钮触发。但是,如果我在初始的fetch仍在进行时单击它,则来自它的数据将加载到新调用的顶部 我使用了window.stop()来停止这种行为,但是有更好的解决方案吗 function updateTable() { fetch('/json') .then((response) => {return response.json()}) .then((response) => {

我使用
fetch
获取json数据,并用结果呈现列表项。包含
fetch
的功能由按钮触发。但是,如果我在初始的
fetch
仍在进行时单击它,则来自它的数据将加载到新调用的顶部

我使用了
window.stop()
来停止这种行为,但是有更好的解决方案吗

function updateTable() {

    fetch('/json')
    .then((response) => {return response.json()})
    .then((response) => { 
        response.records.forEach(el => {
            return '<li>' + el.Name + '</li>';
        })
    })
}

$('button').click(function(){
    window.stop();
    $('body').empty();
    updateTable()
})

这可以通过一个简单的变量来实现。用于确定提取是否仍在处理

var allRecords = [];
var processing = false;

function updateTable() {

    fetch('/json' )
    .then((response) => {return response.json()})
    .then((response) => { 
        processing = false;
        response.records.forEach(el => {
            return '<li>' + el.Name + '</li>';
        })
    })
}

$('button').click(function(){

    if(processing === false){
        processing = true;
        $('body').empty()
        updateTable()
    }
})
var allRecords=[];
var处理=假;
函数updateTable(){
获取('/json')
.then((response)=>{return response.json()})
.然后((响应)=>{
处理=假;
response.records.forEach(el=>{
返回“
  • ”+el.Name+”
  • ”; }) }) } $(“按钮”)。单击(函数(){ 如果(处理===false){ 处理=真; $('body').empty() updateTable() } })
    在加载按钮时禁用该按钮,或在单击按钮时取消挂起的web请求。答案不应编辑到问题中。相反,将正确答案标记为“已接受”。如果您自己找到了解决方案,请写下您自己的答案,并将其标记为已接受。抱歉,我无法添加新答案(问题已被阻止)。为什么要这么做?我相信这会帮助别人的。谢谢。但是,对于大量记录,这仍然意味着不会立即停止
    forEach
    var allRecords = [];
    var processing = false;
    
    function updateTable() {
    
        fetch('/json' )
        .then((response) => {return response.json()})
        .then((response) => { 
            processing = false;
            response.records.forEach(el => {
                return '<li>' + el.Name + '</li>';
            })
        })
    }
    
    $('button').click(function(){
    
        if(processing === false){
            processing = true;
            $('body').empty()
            updateTable()
        }
    })