Skip to main content
Code Review

Return to Question

Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
added 2 characters in body
Source Link
victor
  • 233
  • 1
  • 2
  • 6

In my application I wrote a pagination code that retrieves data from the server as the user scrolls down. In this case, I need to mantain the current page, whether a request is being processed, and whether the server has more data to serve. I didn't want to use globals, but still needed to mantain these 3 variables state.

Here's my code:

fetchNextNotificationPage = (function() {
 var currentPage = 0;
 var maxPageHit = false;
 var fetching = false;
 return function () {
 // Prevent firing concurrent or pointless requests.
 if (fetching || maxPageHit) return;
 fetching = true;
 $.getJSON("GetPageUrl", { page: currentPage + 1 }, function (data) {
 if (!data.erroerror) {
 // Finished fetching, so now it is safe
 // to say we are at previous page + 1.
 currentPage += 1;
 updateData(data.page);
 if (data.lastPage) {
 maxPageHit = true;
 }
 }
 else {
 
 console.log(data.erroerror);
 }
 }).always(function() {.
 fetching = false;
 });
 };
}());

Is there a better way to solve this other than using closure?

In my application I wrote a pagination code that retrieves data from the server as the user scrolls down. In this case, I need to mantain the current page, whether a request is being processed, and whether the server has more data to serve. I didn't want to use globals, but still needed to mantain these 3 variables state.

Here's my code:

fetchNextNotificationPage = (function() {
 var currentPage = 0;
 var maxPageHit = false;
 var fetching = false;
 return function () {
 // Prevent firing concurrent or pointless requests.
 if (fetching || maxPageHit) return;
 fetching = true;
 $.getJSON("GetPageUrl", { page: currentPage + 1 }, function (data) {
 if (!data.erro) {
 // Finished fetching, so now it is safe
 // to say we are at previous page + 1.
 currentPage += 1;
 updateData(data.page);
 if (data.lastPage) {
 maxPageHit = true;
 }
 }
 else {
 
 console.log(data.erro);
 }
 }).always(function() {.
 fetching = false;
 });
 };
}());

Is there a better way to solve this other than using closure?

In my application I wrote a pagination code that retrieves data from the server as the user scrolls down. In this case, I need to mantain the current page, whether a request is being processed, and whether the server has more data to serve. I didn't want to use globals, but still needed to mantain these 3 variables state.

Here's my code:

fetchNextNotificationPage = (function() {
 var currentPage = 0;
 var maxPageHit = false;
 var fetching = false;
 return function () {
 // Prevent firing concurrent or pointless requests.
 if (fetching || maxPageHit) return;
 fetching = true;
 $.getJSON("GetPageUrl", { page: currentPage + 1 }, function (data) {
 if (!data.error) {
 // Finished fetching, so now it is safe
 // to say we are at previous page + 1.
 currentPage += 1;
 updateData(data.page);
 if (data.lastPage) {
 maxPageHit = true;
 }
 }
 else {
 
 console.log(data.error);
 }
 }).always(function() {.
 fetching = false;
 });
 };
}());

Is there a better way to solve this other than using closure?

edited tags; edited title
Link
200_success
  • 145.5k
  • 22
  • 190
  • 479

Static variable Maintaining state in JavaScript anonymous functionsa pagination handler

Post Reopened by Mast , t3chb0t, ferada, janos
deleted 153 characters in body
Source Link
victor
  • 233
  • 1
  • 2
  • 6

I often find myself needing static variables in my event handlers and some other functions and I can't quite decide which is the best way to do this.

In my application I wrote a pagination code that retrieves data from the server as the user scrolls down. In this case, I need to mantain the current page, whether a request is being processed, and whether the server has more data to serve. I didn't want to use globals, but still needed to mantain these 3 variables state.

Here's my code:

fetchNextNotificationPage = (function() {
 var currentPage = 0;
 var maxPageHit = false;
 var fetching = false;
 return function () {
 // Prevent firing concurrent or pointless requests.
 if (fetching || maxPageHit) return;
 fetching = true;
 $.getJSON("GetPageUrl", { page: currentPage + 1 }, function (data) {
 if (!data.erro) {
 // Finished fetching, so now it is safe
 // to say we are at previous page + 1.
 currentPage += 1;
 updateData(data.page);
 if (data.lastPage) {
 maxPageHit = true;
 }
 }
 else {
 
 console.log(data.erro);
 }
 }).always(function() {.
 fetching = false;
 });
 };
}());

Is there a better way to solve this other than using closure?

I often find myself needing static variables in my event handlers and some other functions and I can't quite decide which is the best way to do this.

In my application I wrote a pagination code that retrieves data from the server as the user scrolls down. In this case, I need to mantain the current page, whether a request is being processed, and whether the server has more data to serve. I didn't want to use globals, but still needed to mantain these 3 variables state.

Here's my code:

fetchNextNotificationPage = (function() {
 var currentPage = 0;
 var maxPageHit = false;
 var fetching = false;
 return function () {
 // Prevent firing concurrent or pointless requests.
 if (fetching || maxPageHit) return;
 fetching = true;
 $.getJSON("GetPageUrl", { page: currentPage + 1 }, function (data) {
 if (!data.erro) {
 // Finished fetching, so now it is safe
 // to say we are at previous page + 1.
 currentPage += 1;
 updateData(data.page);
 if (data.lastPage) {
 maxPageHit = true;
 }
 }
 else {
 
 console.log(data.erro);
 }
 }).always(function() {.
 fetching = false;
 });
 };
}());

Is there a better way to solve this other than using closure?

In my application I wrote a pagination code that retrieves data from the server as the user scrolls down. In this case, I need to mantain the current page, whether a request is being processed, and whether the server has more data to serve. I didn't want to use globals, but still needed to mantain these 3 variables state.

Here's my code:

fetchNextNotificationPage = (function() {
 var currentPage = 0;
 var maxPageHit = false;
 var fetching = false;
 return function () {
 // Prevent firing concurrent or pointless requests.
 if (fetching || maxPageHit) return;
 fetching = true;
 $.getJSON("GetPageUrl", { page: currentPage + 1 }, function (data) {
 if (!data.erro) {
 // Finished fetching, so now it is safe
 // to say we are at previous page + 1.
 currentPage += 1;
 updateData(data.page);
 if (data.lastPage) {
 maxPageHit = true;
 }
 }
 else {
 
 console.log(data.erro);
 }
 }).always(function() {.
 fetching = false;
 });
 };
}());

Is there a better way to solve this other than using closure?

added 134 characters in body
Source Link
victor
  • 233
  • 1
  • 2
  • 6
Loading
Post Closed as "Not suitable for this site" by Phrancis, Community Bot, 200_success
edited body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
added 13 characters in body
Source Link
victor
  • 233
  • 1
  • 2
  • 6
Loading
Source Link
victor
  • 233
  • 1
  • 2
  • 6
Loading
default

AltStyle によって変換されたページ (->オリジナル) /