Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.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 - Fatal编程技术网

Javascript 当再次调用JS函数时,它会在更短的时间内开始执行

Javascript 当再次调用JS函数时,它会在更短的时间内开始执行,javascript,jquery,Javascript,Jquery,我需要在一段时间间隔后连续调用一个函数,因此我遵循了本文提供的解决方案 下面是JS代码 function displayLiveLocation() { var location = new Array(); var currentcord = new Array(); $('input[type=checkbox][name="IMSeries[]"]:checked').each(function() {

我需要在一段时间间隔后连续调用一个函数,因此我遵循了本文提供的解决方案

下面是JS代码

    function displayLiveLocation()
    {
        var location = new Array();
        var currentcord = new Array();
        $('input[type=checkbox][name="IMSeries[]"]:checked').each(function() {
           location.push($(this).val()); 
           var current = $(this).val();
           currentcord.push($('#'+current+'_H').val());
        }); 
        copyofAutotrack(currentcord , location);
        setTimeout(function() {
            display();
        }, 15000);

    }
下面是HTML代码

   <div class="checkbox"> 
    <input type="checkbox" onclick="displayLiveLocation()" value="0358911020092058" name="IMEISeries[]" class="IMEISeriesClass[]">       
    <input type="hidden" value="26.79997253418,75.799194335938" id="0358911020092058_H"> 
   </div>

有多个复选框和隐藏字段,其唯一id与上面的类似。单击复选框时,相应的函数以15秒的间隔开始执行。但是,当单击另一个复选框时,也会发生同样的情况,使该函数在调用另一个函数时以较小的时间间隔执行。我希望此函数仅在15秒时执行。任何帮助都将不胜感激。提前谢谢


我不知道如何搜索这个网站,所以我问了这个问题。

每次单击复选框,都会启动一个新的超时。如果用户在几秒钟内单击3个复选框,则会调用3个超时并在另一个超时后不久运行

解决方案是,在调用新超时之前停止最后一个超时,方法是:


当您在执行第一次超时之前的另一时间单击复选框时,第一次单击的超时将在第一次单击后15秒发生,但第二次单击的数据会发生

如果希望它们单独使用自己的数据集,则在调用函数时可以将数据发送到函数中,而不是使用当前数据:

function displayLiveLocation()
{
    var location = new Array();
    var currentcord = new Array();
    $('input[type=checkbox][name="IMSeries[]"]:checked').each(function() {
       location.push($(this).val()); 
       var current = $(this).val();
       currentcord.push($('#'+current+'_H').val());
    }); 
    copyofAutotrack(currentcord, location);
    setTimeout(function() {
        display(currentcord, location);
    }, 15000);

}
如果您想完全停止第一个超时,只需等待第二个超时,您可以使用
clearTimeout

var displayTimeout = null;

function displayLiveLocation()
{
    var location = new Array();
    var currentcord = new Array();
    $('input[type=checkbox][name="IMSeries[]"]:checked').each(function() {
       location.push($(this).val()); 
       var current = $(this).val();
       currentcord.push($('#'+current+'_H').val());
    }); 
    copyofAutotrack();
    if (displayTimeout != null) {
      clearTimeout(displayTimeout);
    }
    displayTimeout = setTimeout(function() {
        displayTimeout = null;
        display(currentcord, location);
    }, 15000);

}

了解如何使用clearTimeout()感谢您提供的解决方案。感谢您提供的解决方案:)
var displayTimeout = null;

function displayLiveLocation()
{
    var location = new Array();
    var currentcord = new Array();
    $('input[type=checkbox][name="IMSeries[]"]:checked').each(function() {
       location.push($(this).val()); 
       var current = $(this).val();
       currentcord.push($('#'+current+'_H').val());
    }); 
    copyofAutotrack();
    if (displayTimeout != null) {
      clearTimeout(displayTimeout);
    }
    displayTimeout = setTimeout(function() {
        displayTimeout = null;
        display(currentcord, location);
    }, 15000);

}