如何检查函数是否执行

how to check whether a function is executed

本文关键字:函数 是否 执行 检查 何检查      更新时间:2023-09-26

我用javascript创建了一个加载器/微调器。旋转功能一直工作到每个过程都结束。

我有一个停止微调器的功能。如果发生任何错误,将不会执行此停止函数。

任何人都可以帮我检查停止功能是否已执行,并在旋转功能在时间限制后未退出时发出警报。

一段JavaScript代码:

function loaderstart()
{
    var x = document.getElementById("loader");
    var xx = document.getElementById("img");
    x.style.display = "block";
    xx.style.display = "block";
    setTimeout(function(){
        if(loaderstop() != true){
            alert("check your network connection");
        }
    },10000);   
}
function loaderstop()
{
    var y = document.getElementById("loader");
    var yy = document.getElementById("img");
    y.style.display = "none";
    yy.style.display = "none";
    return true; 
}

一种简单的方法是设置并检查全局布尔变量。

var hasStopped = false;
function loaderstart()
{
var x = document.getElementById("loader");
var xx = document.getElementById("img");
x.style.display = "block";
xx.style.display = "block";
setTimeout(function(){if(!hasStopped){alert("check your network connection");}},10000);    
}
function loaderstop()
{
hasStopped = true;
var y = document.getElementById("loader");
var yy = document.getElementById("img");
y.style.display = "none";
yy.style.display = "none";
return true; 
}

将超时 ID 存储在变量中并在 loaderstop 中检查它:

var TO = 0;
function loaderstart() {
    var x = document.getElementById("loader");
    var xx = document.getElementById("img");
    x.style.display = "block";
    xx.style.display = "block";
    TO = window.setTimeout(function(){
        alert("check your network connection");
    }, 10000);    
}
function loaderstop()
{
    var y = document.getElementById("loader");
    var yy = document.getElementById("img");
    y.style.display = "none";
    yy.style.display = "none";
    if (TO) {
        window.clearTimeout(TO);
    }
    return true; 
}

window.clearTimeout()