0

I already spend my time to knowing why my variable is cannot be reassign when my ajax response is successfull, but the result is still giving nothing happen with my variable value, I already do some trick to reassign my variable value but the result is still same nothing changed, here my code :

setTimeout(() => {
   let scrollHeight = 1500;
   $(".my-class-content-scroll").animate({scrollTop: scrollHeight}, 5000);

   $(".my-class-content-scroll").on('scroll', function () {
       let newScrollHeight = 0 ;

       if (scrollHeight >= 1500) {
          $.ajax({
            url: `/my-url`,
            method: "GET",
            dataType: "JSON",
            success: function (success) {
               /* my process when response is success */
               /* start reassign the value */
               const newValue = $(".my-class-content-scroll").prop("scrollHeight");
               newScrollHeight = newValue
            }
          });
       }
       console.log(newScrollHeight) /* <= the result is giving 0 value */
   })
}, 1000)

for another trick I try to reassign the variable value with the function, like this :

setTimeout(() => {
   let scrollHeight = 1500;
   $(".my-class-content-scroll").animate({scrollTop: scrollHeight}, 5000);

   $(".my-class-content-scroll").on('scroll', function () {
       let newScrollHeight = 0 ;
       
       function setScrollHeight (height) {
           newScrollHeight = height
       }

       if (scrollHeight >= 1500) {
          $.ajax({
            url: `/my-url`,
            method: "GET",
            dataType: "JSON",
            success: function (success) {
               /* my process when response is success */
               /* start reassign the value */
               const newValue = $(".my-class-content-scroll").prop("scrollHeight");
               setScrollHeight(newValue)
            }
          });
       }
       console.log(newScrollHeight) /* <= the result is still same than before and giving 0 value */
   })
}, 1000)

I hope anyone can explain to me why my method is not working if anyone found the answer about my problem, thank you good people

ProLuck
  • 331
  • 4
  • 18

0 Answers0