An warware: ƙarewar lokaci

A cikin duniyar shirye-shirye da ci gaban yanar gizo, sanin yadda ake tafiyar da al'amuran da suka wuce lokaci yana da mahimmanci. Kurakurai na iya tasowa saboda dalilai daban-daban kamar rashin jinkirin hanyar sadarwa, lodin uwar garken da dai sauransu. Dabarar tafiyar da lokacin ƙarewa ya zama ƙashin baya ga ƙarfin shirin.

Don fahimtar ɓata lokaci da kyau, bari mu yi la'akari da yanayin injin mota. Idan kun kunna maɓallin kunnawa, kuna tsammanin injin zai fara aiki cikin ƙasa da daƙiƙa kaɗan. Idan ba haka ba, kuna iya jira na wasu daƙiƙa biyu kafin kashe wuta da ƙoƙarin sake kunna injin ɗin. Wannan lokacin ƙofa kafin yanke shawarar tsayawa da sake farawa yayi daidai da waɗanne lokutan ƙarewa ne a duniyar shirye-shirye.

Maganin Typescript

Nau'in rubutu, babban saitin JavaScript da aka buga, yana ba masu haɓaka kayan aikin da ake buƙata don gudanar da lokacin ƙarewa yadda ya kamata. Don sarrafa lokacin ƙarewa, mun ƙirƙiri alƙawari wanda ko dai zai warware cikin nasara a cikin ƙayyadadden lokacin da aka ƙayyade ko ƙi idan ya wuce. Ga misali:

function timeout(ms: number, promise: Promise<any>): Promise<any> {
    return new Promise(function(resolve, reject) {
        setTimeout(function() {
            reject('Timed out in '+ ms + 'ms.');
        }, ms);
        promise.then(resolve);
    });
}

Fahimtar Code

Masu haɓakawa sau da yawa suna samun sauƙin fahimtar matsaloli lokacin da aka rarraba maganin zuwa ƙananan guntu masu iya sarrafawa.

Ma'ana 1: Da farko, muna ƙirƙiri wani aiki da ake kira 'timeout' wanda ke karɓar 'ms' da 'alƙawari' azaman sigogi. 'ms' shine ƙayyadaddun lokaci a cikin milli seconds, kuma 'alƙawari' shine ainihin alkawarin da muke ƙoƙarin aiwatarwa.

function timeout(ms: number, promise: Promise<any>)

Batu 2: A cikin aikin 'lokacin ƙarewa', muna dawo da sabon alƙawari tare da 'yankewa' da 'ƙi' sigogi.

return new Promise(function(resolve, reject)

Batu na 3: Muna amfani da setTimeout don haifar da 'ƙin yarda' idan iyakarmu (ms) ta kai kafin a warware alkawari.

setTimeout(function() {
            reject('Timed out in '+ ms + 'ms.');
        }, ms);

Batu na 4: Idan ainihin alkawari ya warware kafin ms, kawai mu ƙaddamar da ƙuduri tare da 'alƙawari.sa'an nan (sanya)'

promise.then(resolve);

Don haka, ta hanyar aiwatar da ƙarewar lokaci, za mu iya tabbatar da lambar mu ba ta jira har abada don amsawa ba.

Dakunan karatu masu alaƙa da Ayyuka

Nau'in rubutu baya zuwa tare da ɗakin karatu da aka riga aka gina don sarrafa lokacin ƙarewa. Koyaya, ya dace saboda alaƙar sa ta kut-da-kut da JavaScript da ɗimbin yanayin yanayin npm. Wasu ayyukan JavaScript masu alaƙa sune:

  • SaitaTimeout(): Wannan wani aiki ne, wani ɓangare na Abubuwan Taga Lokaci, wanda ke dakatar da aiwatar da lambar don ƙayyadadden adadin lokaci sannan kuma yana gudanar da lambar.
  • ClearTimeout(): Wannan aikin yana dakatar da aiwatar da lambar da aikin setTimeout() ya jinkirta.
  • SetInterval(): Kama da setTimeout() a cikin aiki, amma maimakon aiwatar da lambar sau ɗaya bayan wani ɗan lokaci, setInterval() yana gudanar da lambar akai-akai bayan saita lokaci har sai an kira clearInterval().
  • ClearInterval(): Ana amfani da wannan aikin don dakatar da lambar da setInterval() ta saita daga aiki.

Ƙarfin sarrafa lokacin ƙarewa cikin alheri a lambar Typescript yana da girma. Ko da yake in mun gwada da sauki, fahimta da kuma amfani da dace lokaci-lokaci hanyoyin ba kawai tabbatar da santsi gwaninta na mai amfani amma kuma kara habaka tasiri da gwaninta na shirin.

Shafi posts:

Leave a Comment