最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Clear array of setTimeout's - Stack Overflow

programmeradmin5浏览0评论

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

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.

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.

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

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.

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.

Share Improve this question asked Oct 23, 2012 at 14:47 Jacob TomlinsonJacob Tomlinson 3,7733 gold badges35 silver badges66 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 16

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

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

As usual just typing the question has made me seen the error of my ways.

The problem es from me setting the array keyword to some predefined value and then when I loop I'm looking for numeric keys. To overe this I've modified

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

to be

function showtooltip(uniqueid, delay){
    tuttimer.push(setTimeout(function(){
        //Create tooltip code here
    },delay));
}
发布评论

评论列表(0)

  1. 暂无评论