function runInterval() {
intervalFun = setInterval(qrcodeRequest, 5000);
}
code
function stopInterval() {
clearInterval(intervalFun);
document.getElementById("qrcode").classList.remove("d-block");
document.getElementById("qrcode").classList.add("d-none");
document.getElementById("spinner").classList.remove("d-none");
}
let intervalFun2;
function runInterval2() {
qrcodeRequest2();
intervalFun2 = setInterval(qrcodeRequest2, 5000);
}
function stopInterval2() {
clearInterval(intervalFun2);
document.getElementById("qrcode2").classList.remove("d-block");
document.getElementById("qrcode2").classList.add("d-none");
document.getElementById("spinner2").classList.remove("d-none");
}
Assistant
It looks like you're managing two similar sets of functions (runInterval and runInterval2, along with their corresponding stop functions) that handle intervals for making QR code requests and updating the DOM elements accordingly. The code for both sets of functions is nearly identical except for the variable names and the DOM element IDs.
To improve the maintainability and reduce redundancy in your code, you can refactor these functions to be more generic by parameterizing the differences between them. This way, you can have a single pair of runInterval and stopInterval functions that handle both cases based on parameters.
Here's how you can refactor your code:
javascript
let intervalFun;
function runInterval(requestFunction, qrcodeId, spinnerId) {
requestFunction();
intervalFun = setInterval(requestFunction, 5000);
document.getElementById(qrcodeId).classList.remove("d-none");
document.getElementById(qrcodeId).classList.add("d-block");
document.getElementById(spinnerId).classList.add("d-none");
}
function stopInterval(qrcodeId, spinnerId) {
clearInterval(intervalFun);
document.getElementById(qrcodeId).classList.remove("d-block");
document.getElementById(qrcodeId).classList.add("d-none");
document.getElementById(spinnerId).classList.remove("d-none");
}
Then, you can call these functions with the appropriate parameters:
javascript
// For the first QR code request
function startFirstQRCode() {
runInterval(qrcodeRequest, "qrcode", "spinner");
}
function stopFirstQRCode() {
stopInterval("qrcode", "spinner");
}
// For the second QR code request
function startSecondQRCode() {
runInterval(qrcodeRequest2, "qrcode2", "spinner2");
}
function stopSecondQRCode() {
stopInterval("qrcode2", "spinner2");
}
Explanation:
Parameterization: By passing the request function (qrcodeRequest or qrcodeRequest2) and the IDs of the DOM elements ("qrcode", "spinner", etc.) as parameters, the runInterval and stopInterval functions become flexible and can be reused for both cases.
Single Interval Variable: If these intervals need to run independently, you should have separate interval variables for each one. In that case, you can modify the functions to accept and return the interval variables.
Here's an improved version that handles multiple intervals independently:
let intervalFun1;
let intervalFun2;
// For the first QR code request
function startFirstQRCode() {
intervalFun1 = runInterval(qrcodeRequest, "qrcode", "spinner");
}
function stopFirstQRCode() {
stopInterval(intervalFun1, "qrcode", "spinner");
}
// For the second QR code request
function startSecondQRCode() {
intervalFun2 = runInterval(qrcodeRequest2, "qrcode2", "spinner2");
}
function stopSecondQRCode() {
stopInterval(intervalFun2, "qrcode2", "spinner2");
}
Benefits of Refactoring:
Reduced Redundancy: Eliminates duplicate code, making it easier to maintain and less prone to errors.
Enhanced Readability: Simplifies the code structure, making it more readable and understandable.
Scalability: Makes it easier to add more QR code intervals in the future by reusing the same functions with different parameters.
Additional Suggestions:
Use Classes for DOM Manipulation: If you're frequently toggling classes, consider using classList.toggle() or manipulating classes more elegantly.
javascript
function toggleDisplay(elementId, show) {
const element = document.getElementById(elementId);
element.classList.toggle("d-none", !show);
element.classList.toggle("d-block", show);
}
Error Handling: Ensure that your requestFunction handles errors and edge cases, especially if it involves asynchronous operations like network requests.
Use Let or Const Appropriately: Use let for variables that will be reassigned and const for variables that won't change, such as function declarations.
Final Refactored Code:
javascript
function runInterval(requestFunction, qrcodeId, spinnerId) {
requestFunction();
const interval = setInterval(requestFunction, 5000);
toggleDisplay(qrcodeId, true);
toggleDisplay(spinnerId, false);
return interval;
}
function stopInterval(interval, qrcodeId, spinnerId) {
clearInterval(interval);
toggleDisplay(qrcodeId, false);
toggleDisplay(spinnerId, true);
}
function toggleDisplay(elementId, show) {
const element = document.getElementById(elementId);
element.classList.toggle("d-none", !show);
element.classList.toggle("d-block", show);
}
let intervalFun1;
let intervalFun2;
// Start and stop functions for the first QR code
function startFirstQRCode() {
intervalFun1 = runInterval(qrcodeRequest, "qrcode", "spinner");
}
function stopFirstQRCode() {
stopInterval(intervalFun1, "qrcode", "spinner");
}
// Start and stop functions for the second QR code
function startSecondQRCode() {
intervalFun2 = runInterval(qrcodeRequest2, "qrcode2", "spinner2");
}
function stopSecondQRCode() {
stopInterval(intervalFun2, "qrcode2", "spinner2");
}
Usage:
Call startFirstQRCode() or startSecondQRCode() to start the intervals.
Call stopFirstQRCode() or stopSecondQRCode() to stop them.
By refactoring your code in this way, you create a more maintainable and scalable codebase. If you have further questions or need assistance with other parts of your code, feel free to ask!