I'm developing an userscript that works with links. It should perform some code when user clicks on a link(right or left click). The code is sending ajax request to a server and based on the response might change the link href attrubute in some cases, so user shouldn't use the link while it isnt finished. Im delaying a left click by custom event handler and it works fine, but I couldnt find way to delay the standard dropdown menu, only to disable it completely.

I would really appreciate any help.

有帮助吗?

解决方案

This might be one of the few valid use cases for a synchronous AJAX request.

If you're using XMLHttpRequest directly, you can make a synchronous request by passing false as the third parameter to the .open() method:

var request = new XMLHttpRequest();
request.open('GET', '/bar/foo.txt', false);  // `false` makes the request synchronous
request.send(null);

if (request.status === 200) {
  console.log(request.responseText);
}

(Example code from the MDN page linked above.)

If you're using jQuery, you can make a synchronous request by passing the option async: false to jQuery.ajax(). See jQuery: Performing synchronous AJAX requests for more details.

However, note that, in general, synchronous AJAX requests are almost always a bad idea. As long as your server (and your Internet connection) handles requests very quickly and reliably, it might work acceptably well, but if your server experiences heavy load, or your connection starts dropping packets, having to wait even a few seconds for any mouse click to have an effect could quickly get annoying.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top