There is such ajax function

function searchPromokode(){ var promokode= $(".promokode").val(); if(promokode.length>13){ $.ajax({ type: 'POST', url: "/search/promokode", data: "promokode="+promokode, success: function(answer) { if (answer){ alert('ΠŸΡ€ΠΎΠΌΠΎΠΊΠΎΠ΄ Π°ΠΊΡ‚ΠΈΠ²ΠΈΡ€ΠΎΠ²Π°Π½'); } else { alert('ΠŸΡ€ΠΎΠΌΠΎΠΊΠΎΠ΄ ΡƒΠΆΠ΅ Π±Ρ‹Π» Π°ΠΊΡ‚ΠΈΠ²ΠΈΡ€ΠΎΠ²Π°Π½'); } } }); } } 

I want to put a loader let's say

 <div class="loader"></div> .loader{ display:none; } 

There are two questions at what point to put this loader in the ajax request. And second, I certainly understand that we all want to speed up the process of all the work, but as I understand it, the processing will be very fast, less than a second and the user will not see the loader, sorry for the dumb question BUT how to set a delay of 1-2 seconds so that the loader spins)

  • should not be put in the request, but in front of it. and on completion to clean. Artificial increase in waiting time xs why you need it. - teran

1 answer 1

If you want the loader to even slightly twist, you can add a moment of success to it, and make the result of success with a delay through setTimeout, but in general, loaders usually put the ajaxSend event

 function searchPromokode(){ var promokode= $(".promokode").val(); if(promokode.length>13){ $.ajax({ type: 'POST', url: "/search/promokode", data: "promokode="+promokode, success: function(answer) { // Ρ‚ΡƒΡ‚ добавляСм Π»ΠΎΠ°Π΄Π΅Ρ€ setTimeout(function () { // Ρ‚ΡƒΡ‚ ΡƒΠ±ΠΈΡ€Π°Π΅ΠΌ Π»ΠΎΠ°Π΄Π΅Ρ€ if (answer){ alert('ΠŸΡ€ΠΎΠΌΠΎΠΊΠΎΠ΄ Π°ΠΊΡ‚ΠΈΠ²ΠΈΡ€ΠΎΠ²Π°Π½'); } else { alert('ΠŸΡ€ΠΎΠΌΠΎΠΊΠΎΠ΄ ΡƒΠΆΠ΅ Π±Ρ‹Π» Π°ΠΊΡ‚ΠΈΠ²ΠΈΡ€ΠΎΠ²Π°Π½'); }}, 2000); } }); } } 
  • it's kind of fun to turn on the loader, when the result of the query has already come back, don't you think so? : D you need to turn it on before sending the request, then it will spin exactly as long as the request is executed. - teran
  • You first read the vehicle's question, then carefully my answer before the code, and then think about whether it was worth leaving this comment - Peresada
  • it does not interfere. the request can be executed for some time, and at that moment the user will have a white screen. in any case, even though there is no delay, the loader should be shown before the request itself. If the minimum display time is required, add a timestamp and check how long the request was executed, taking this time out of the desired 2 seconds. - teran
  • Although on the other hand, you mentioned ajaxSend , it’s not clear why you didn’t implement it this way - teran
  • because judging by the code and the question itself, the author does not have a task in functionality, and in this case, it’s β€œI want the loader to spin longer, because it’s beautiful and effective”, if such a task arises, he will be able to drive google ajaxSend and find out more about it. And in my answer, the task was rather to show the principle and indicate in which direction to dig, than to show the code itself - Peresada