多次调用一个函数,已经运行
Call several times a function, already running
我使用 dropzone 库上传文件,上传文件后,我调用一个函数来检索服务器上的文件列表。
问题是,当我导入小文件时,检索文件的函数被执行 "at the same time",更准确地说:对该函数的调用已经是 运行。
我想要的是放一个标志来限制对这个函数的访问,只有当它完成时。
//Listener on add file
dropzone.on("complete", function (file)
ajaxListFiles();
});
谢谢!
您可以在函数启动时设置一个 running
标志,并在函数结束时取消设置。然后在再次执行函数之前检查这个标志:
let running = false;
dropzone.on("complete", function (file)
if (!running) {
ajaxListFiles();
}
});
function ajaxListFiles() {
running = true; // at the very beginning of this function
// ... your code
running = false; // when it ends, so probably in some callback, not necessary at the end of this function
}
我使用 dropzone 库上传文件,上传文件后,我调用一个函数来检索服务器上的文件列表。 问题是,当我导入小文件时,检索文件的函数被执行 "at the same time",更准确地说:对该函数的调用已经是 运行。
我想要的是放一个标志来限制对这个函数的访问,只有当它完成时。
//Listener on add file
dropzone.on("complete", function (file)
ajaxListFiles();
});
谢谢!
您可以在函数启动时设置一个 running
标志,并在函数结束时取消设置。然后在再次执行函数之前检查这个标志:
let running = false;
dropzone.on("complete", function (file)
if (!running) {
ajaxListFiles();
}
});
function ajaxListFiles() {
running = true; // at the very beginning of this function
// ... your code
running = false; // when it ends, so probably in some callback, not necessary at the end of this function
}