如何检查函数是否执行

时间:2022-09-15 16:58:21

I have created a loader/spinner using javascript. The spinning function works till every process is over.

我用javascript创建了一个loader / spinner。旋转功能一直有效,直到每个过程结束。

I have a function to stop the spinner . If any error occurs this stopping function will not be executed.

我有一个功能来阻止微调器。如果发生任何错误,则不会执行此停止功能。

Can anybody help me to check whether the stopping function is executed or not and to alert if the spinning function doesn't exit after a time limit.

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

A piece of javascript code:

一段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; 
}

2 个解决方案

#1


2  

One simple way you could do is by setting and checking a global boolean variable.

您可以做的一种简单方法是设置和检查全局布尔变量。

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; 
}

#2


0  

Store timeout ID in variable and check it inside loaderstop:

将超时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()

window.clearTimeout()

#1


2  

One simple way you could do is by setting and checking a global boolean variable.

您可以做的一种简单方法是设置和检查全局布尔变量。

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; 
}

#2


0  

Store timeout ID in variable and check it inside loaderstop:

将超时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()

window.clearTimeout()