Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
571 views
in Technique[技术] by (71.8m points)

javascript - 如何在浏览器控制台中用一行代码多次执行javascript命令?(How to execute a javascript command multiple times with single line of code in browser console?)

I want to check if all ajax requests have completed and only then interact with element on a web page.

(我想检查是否所有ajax请求都已完成,然后才与网页上的元素进行交互。)

Hence I opened chrome browser's developer console.

(因此,我打开了chrome浏览器的开发者控制台。)

And after entering url in search bar and hitting ENTER key, in console I keep hitting $.active multiple times and in results most of the time I can see returned value as 0 but some time as 1

(在搜索栏中输入url并按ENTER键后,在控制台中,我多次保持$.active ,并且在大多数情况下,我可以看到返回的值为0但有时为1)

I hit $.active command with hand hence I am not able to see consistent result all the time.

(我用手打了$.active命令,因此无法始终看到一致的结果。)

Is there any java script code which I can enter in console and after hitting it, $.active command will be hit one by one till the number of times mentioned?

(是否有任何我可以在控制台中输入的Java脚本代码,点击它后, $.active命令将被一一命中,直到提到的次数为止?)

  ask by TDHM translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If $ is jQuery, you can add an ajaxComplete callback, and check if $.active inside:

(如果$是jQuery,则可以添加ajaxComplete回调,并检查$.active在其中:)

$(document).on('ajaxComplete', () => {
  Promise.resolve().then(() => {
    if (!$.active) {
      console.log('All done');
    }
  });
});

It's not a single line of code, but it should work.

(它不是一行代码,但是应该可以。)

(You can test it by running this in the console, then opening your inbox or reputation record in Stack Overflow's topbar. An ajax request will be sent out, and when it's complete, ajaxComplete will run, and $.active will get set to 0)

((您可以通过在控制台中运行它来进行测试,然后在Stack Overflow的ajaxComplete打开收件箱或信誉记录ajaxComplete请求将被发送出去,完成后, ajaxComplete将运行,并且$.active将被设置为0 ))

The Promise.resolve().then is needed because $.active gets decremented only after the ajaxComplete callback runs.

(Promise.resolve().then是必需的,因为$.active ajaxComplete回调运行后才递减。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...