简体   繁体   中英

How to put a timestamp within a variable name in javascript (e.g. var a12439853 = 1)

I have made a CSS3 ajax loader using this code. id is the location of the ajaxloader. This code works fine and the loader functions properly, however if the function is called twice, the Timeout's will cancel each other out because the timeout is assigned to a variable. Somehow, I want to make sure that this will never happen. Here is the javascript I am using

function ajaxloader(id) {
    var i = $("#" + id + " .ajaxpieces").length;
    var s = $("#" + id + " .ajaxpieces").filter(function() {
        return ($(this).css('background-color') == "rgb(0, 128, 0)");
    }).next();

    if (s.length < 1) {
        s = $("#" + id + " .ajaxpieces").first();
    }
    s.css('backgroundColor','green').siblings().css('backgroundColor','grey');
    ajax_ii = setTimeout(function(){ajaxloader(id);},550);
}

function killloader() {
    clearTimeout(ajax_ii);
}

Since every variable are object of window, you can do it this way:

            window['a_time'+new Date().getTime()] = setTimeout(function(){
                ajaxloader(id);
            },550);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM