我需要在一个时间间隔后连续调用一个函数,因此我遵循了此链接上提供的解决方案。
这是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秒时执行。任何帮助,将不胜感激。提前致谢。
我不知道如何在此网站上搜索此信息,所以我问了一个问题。
当您在第一次超时之前再次单击该复选框时,第一次单击的超时将在第一次单击后的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);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句