Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Several MyObjList objects can be created by the user. If the load_stream function becomes a member function of MyobjList, multiple getJSON functions are executed, which I don't want.
I want to define the load_Stream function externally so that it only runs once at a time.
The goal is to stop getJSON in progress when a new load stream is requested and start a new request.
When the following code is executed, an error occurs in the get_myValue function of MyObjList as shown below.
how can i solve this problem?

What I have tried:

var myList; /* return value from load_stream function. I know it's no good about this is global variation. This is an just example*/

var load_stream = function( args ){
    $.getJSON("http://..."), args, function( result ){
       myList = result.myReturnValue;       
       return myList
    }).fail(function( jqXHR, error ){
       console.log(',,,');
    })
    return false;
};

var Mediator = function(){
   let MyObj = [],
       length = '0';       
   return {
      addObj: function(){
         var newMyObj = object.create(MyObjList());
         MyObj.push(newMyObj);
         length = MyObj.length;
         MyObj[length].get_myValue():
      }
   }
}

var MyObjList = function(){
   var id,
       page,
       publish;
    return {
        get_myValue: function(){
             const jsonData = {'pID': id, 'cID': publish};
             /* Uncaught RefferenceError: load_stream is not defined */
             page = load_stream(jsonData);
        }        
    }
}
Posted
Updated 4-Dec-22 22:57pm

1 solution

It's not entirely clear what you're trying to do here.

If you want to abort one AJAX request when another one starts, you'll want to use an AbortController:
AbortController - Web APIs | MDN[^]

That means you'll probably want to switch to the fetch API:
Fetch API - Web APIs | MDN[^]

To simplify the code, you'll probably also want to use an async function:
async function - JavaScript | MDN[^]

JavaScript
let abortController = null;

const load_stream = async (data) => {
    if (abortController) { abortController.abort(); }
    
    const url = new URL("https://...");
    if (data) {
        Object.entries(data).forEach(entry => {
            const [key, value] = entry;
            url.searchParams.set(key, value);
        });
    }
    
    abortController = new AbortController();
    try {
        const response = await fetch(url, { signal: abortController.signal });
        if (!response.ok) {
            console.error(response.status, response.statusText);
            return [];
        }
        
        const result = await response.json();
        return result.myReturnValue;
    } catch (e) {
        console.error(e);
        return [];
    }
};

const MyObjList = function(){
   let id,
       page,
       publish;
    
    return {
        get_myValue: async function() {
             const jsonData = { 'pID': id, 'cID': publish };
             page = await load_stream(jsonData);
        }        
    }
};

const Mediator = function(){
   let MyObj = [];
   return {
      addObj: async function(){
         const newMyObj = object.create(MyObjList());
         MyObj.push(newMyObj);
         await newMyObj.get_myValue():
      }
   }
};
 
Share this answer
 
Comments
Chopin2001 5-Dec-22 16:16pm    
Thank you very much. Your code teaches more than it solves the problem. This is important to me. Because it is a great help to me studying alone. Because it makes me feel what I lack. Thank you very much. I appreciate your job.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900