清除setTimeout的数组 [英] Clear array of setTimeout's

查看:108
本文介绍了清除setTimeout的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我在我的AJAX应用程序中有一个功能,它在一定时间后将工具提示放在角落里,以帮助提示用户以及他们正在做什么。还有第二个函数可以在用户点击其他位置时清除超时,因为工具提示不再相关。

Ok so I have a function in my AJAX application which sticks a tooltip in the corner after a certain amount of time to help prompt the user along with what they're doing. There is also a second function which clears the timeout if the user clicks somewhere else as that tooltip wont be relevant anymore.

我现在开始遇到设置问题超时的多个工具提示,设置它们很好但是如果用户继续,我找不到有效的方法取消它们。

I'm starting to have an issue now with setting multiple tooltips on timeouts, setting them is fine but I can't find an efficient way to cancel them if the user moves on.

目前我的代码看起来像这样

Currently my code looks like this

var tuttimer = new Array();

function showtooltip(uniqueid, delay){
    tuttimer[uniqueid] = setTimeout(function(){
        //Create tooltip code here
    },delay);
}

function clearTuttimer(){
    if(typeof tuttimer != 'undefined'){
        for (var i = 0; i < tuttimer.length; i++) {
            clearTimeout(tuttimer[i]);
        }
    }
}

所以 tuttimer 数组是在页面加载时创建的,然后每当用户执行某些工具提示以显示 showtooltip()函数时并且给出了唯一的id和延迟时间。

So the tuttimer array is created at page load and then whenever a user does something which would case a tooltip to display the showtooltip() function is called and is given a unique id and a delay time.

但是如果用户继续使用其他东西它会调用函数 clearTuttimer() 检查数组是否存在,然后循环并清除每个单独的超时。

But if the user were to move on to something else it calls the function clearTuttimer() which checks to see if the array exists and then loops through and clears each individual timeout.

但是这不起作用。希望有人可以指出我正确的方向。非常感谢。

However this isn't working. Hopefully someone can point me in the right direction. Many thanks.

推荐答案

如果你使用数组,那么使用 Array.push 方法。

If you use array, then use Array.push method.

var tuttimer = [];

function showtooltip(delay){
    tuttimer.push(setTimeout(function(){
        //Create tooltip code here
    },delay));
}

function clearTuttimer(){
    for (var i = 0; i < tuttimer.length; i++) {
        clearTimeout(tuttimer[i]);
    }
}

如果你想使用 uniqueid ,然后使用对象而不是数组。

If you want to use uniqueid, then use an object instead of array.

var tuttimer = {};

function showtooltip(uniqueid, delay){
    tuttimer[uniqueid] = setTimeout(function(){
        //Create tooltip code here
    },delay);
}

function clearTuttimer(){
    for (var k in tuttimer) {
        clearTimeout(tuttimer[k]);
    }
}

这篇关于清除setTimeout的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆