How can I show a list of every thread running spawned by setTimeout/setInterval

后端 未结 3 688
悲&欢浪女
悲&欢浪女 2021-02-01 22:11

I want to do this either by pure javascript or any sort of console in a browser or whatever.

Is it possible?

Thanks

Further explanations: I want to debug

3条回答
  •  鱼传尺愫
    2021-02-01 22:33

    Finally done, it was interesting for me so I spent some time trying to come up with something, and here it's

    It overrides browser's setTimeout and fill active status of current active calls in window._activeSetTimeouts hash, with window._showCurrentSetTimeouts() demo function that displays current setTimeout calls that are waiting.

    if(typeof window._setTimeout =='undefined') {
    window._setTimeout=window.setTimeout;
    
    window._activeSetTimeouts={};
    window._activeSetTimeoutsTotal=0;
    window._setTimeoutCounter=0;
    window._showCurrentSetTimeouts=function() {
        var tgt=document.getElementById('_settimtouts');
        if(!tgt) {
        tgt=document.createElement('UL');
        tgt.style.position='absolute';
        tgt.style.border='1px solid #999';
        tgt.style.background='#EEE';
        tgt.style.width='90%';
        tgt.style.height='500px';
        tgt.style.overflow='auto';
        tgt.id='_settimtouts';
    
        document.body.appendChild(tgt);
        }
    
        tgt.innerHTML='';
        var counter=0;
        for(var i in window._activeSetTimeouts) {
            var li=document.createElement('LI');
            li.innerHTML='[{status}] {delay} ({calltime})
    {cb}
    '.f(window._activeSetTimeouts[i]); li.style.background=(counter++%2)?'#CCC' : '#EEB'; tgt.appendChild(li); } } window.setTimeout=function(cb, delay) { var id = window._setTimeoutCounter++; var handleId = window._setTimeout(function() { window._activeSetTimeouts[id].status='exec'; cb(); delete window._activeSetTimeouts[id]; window._activeSetTimeoutsTotal--; }, delay); window._activeSetTimeouts[id]={ calltime:new Date(), delay:delay, cb:cb, status:'wait' }; window._activeSetTimeoutsTotal++; return id; } //the following function is for easy formatting String.prototype.f=function(obj) { var newStr=this+''; if(arguments.length==1) { if(typeof(obj)=='string') { obj={x:obj}; } for(var i in obj) { newStr=newStr.replace(new RegExp('{'+i+'}', 'g'), obj[i]+''); } newStr+=''; } else { for(var i=0; i

提交回复
热议问题