简体   繁体   English

如何在JavaScript的变量名中放置时间戳(例如var a12439853 = 1)

[英]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. 我已经使用此代码制作了CSS3 ajax加载器。 id is the location of the ajaxloader. id是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 这是我正在使用的javascript

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的对象,因此您可以按照以下方式进行操作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在特定的JavaScript代码中加载外部HTML页面? 例如,在JS函数中 - How to load an external HTML page within a specific JavaScript code? E.g. Within a JS function 带有多个逗号分隔值的变量声明是什么意思(例如var a = b,c,d;) - What does variable declaration with multiple comma separated values mean (e.g. var a = b,c,d;) JavaScript:创建对'this'的引用(例如'var _this = this')与bind / call / apply - JavaScript: creating a reference to 'this' (e.g. 'var _this = this') vs. bind/call/apply 当该元素及其类名是动态创建的(例如 document.createElement)时,如何将 div 放入文档中? - How to put a div into the document when that element and its class name are dynamically created (e.g. document.createElement)? 将时间戳转换为相对日期/时间(例如,“2分钟前”)并使用Javascript不断更新它 - Converting a timestamp to a relative date/time (e.g., “2 minutes ago”) and continuously updating it with Javascript 如何在Javascript中将数组显示为视觉图像(例如迷宫) - How to display arrays as visuals in Javascript (e.g. for a maze) 在标记字符串中,如何用自己的标题文本替换任何例如类名指定的元素? - Within a markup-string, how to replace any e.g. class-name specified element with its own title-text? 在JavaScript中,如何从像素值(例如2em或20px)获取单位(例如em或px)? - In javascript, how to get the unit (e.g. em or px) from a pixel value (e.g. 2em or 20px)? 我可以对JavaScript设置时间限制(例如,使用iframe)吗? - Can I put impose a time limit on JavaScript (e.g., using an iframe)? 用javascript控制视频(例如streamcloud) - Control Videos with javascript (e.g. streamcloud)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM