У меня есть следующий код
<html>
<head>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
</head>
<body style = "background-color:yellow;width:100%;height:100%;">
<a href = "javascript:test()">test</a>
<script>
function test() {
$("body").css("cursor","wait"); //cursor must be changed here
$.ajax(...);
}
</script>
</body>
</html>
Проблема с этим кодом заключается в том, что курсор меняется с default на wait в браузере только после завершения функции test(), но мне нужно, чтобы он изменился в определенной точке функции. Я пробовал использовать FF 58 в Ubuntu 14 и Opera в Windows 7. Я читал много сообщений об этом и пробовал это решение.
$(document).ajaxStart(function() {
$(document.body).css({'cursor' : 'wait'});
}).ajaxStop(function() {
//$(document.body).css({'cursor' : 'default'});
});
но безуспешно. Как это исправить? Любые идеи?
@jonhid я редактировал
Я тоже свой вопрос редактировал! :-)
@jonhid Да, я показал в коде, в какой точке. См. Функцию test ().
Вы используете jQuery для вызова ajax?
@jonhid Да, я только что добавил его в функцию.



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Вы можете использовать перед отправкой следующим образом:
$.ajax({
beforeSend: function(){
// Handle the beforeSend event
},
complete: function(){
// Handle the complete event
}
// ......`enter code here`
});ИЛИ используйте when и timeout:
<!DOCTYPE html>
<html>
<head>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function ajaxready() {
setTimeout(function() {
$.ajax({
url: "https://www.w3schools.com/jquery/demo_test.txt",
beforesend: function() {},
success: function(result) {
$("#div1").html(result);
$("body").css("cursor", "");
}
});
}, 500);
}
function loading() {
$("body").css("cursor", "wait"); //cursor must be changed here
}
$(document).ready(function() {
$("button").click(function() {
$.when(loading()).done(function() {
ajaxready();
});
});
});
</script>
<style>
button {
cursor: inherit
}
</style>
</head>
<body>
<div id = "div1">
<h2>Let jQuery AJAX Change This Text</h2>
</div>
<span></span>
<button>Get External Content</button>
</body>
</html> $(document).ajaxStart(function (event, jqxhr, settings) {
$(document.body).css({ 'cursor': 'wait' });
});
$(document).ajaxComplete(function (event, jqxhr, settings) {
$(document.body).css({ 'cursor': 'normal' });
});
$(document).ajaxError(function (event, request, settings) {
$(document.body).css({ 'cursor': 'normal' });
});
function AjaxCall() {
$.ajax({
type: "POST",
url: '/home/AjaxURL',
contentType: "application/json; charset=utf-8",
global: true,// this makes sure ajax set up ajaxStart/ajaxComplete/ajaxerror will triggered
dataType: "json",
success: function () { },
error: function () { }
});
}
$(document).ready(function () {
AjaxCall();
});
Примечание. Установка курсора на document.body, как и вы, означает, что он будет применяться только в документе и не будет применяться в других элементах dom, таких как привязка, текстовое поле и т. д.
Я нашел ответ. У меня был в ajax async:false -
$.ajax({ ...
async: false,
...
});
Когда я перешел на
$.ajax({ ...
async: true,
...
});
все заработало как положено.
И каково желаемое поведение ?? Вы сказали: «Мне нужно, чтобы он изменился в определенной точке функции». Вы имеете в виду, возможно, до вызова ajax?