Solution for network disconnection and reload during WeChat development

Source: Internet
Author: User
Analyze {code ...} click "reload" to reload the page. as page rendering is inseparable from interface requests during Project creation, I encapsulated wx. request to determine whether the network is disconnected, and record this request after the network is disconnected {code ...} application {code ...} when the network is disconnected, execute this. re... analysis
The applet does not currently provide a refresh API. Therefore, you need to record the current operation and click refresh to execute it again.
Effect

Click "reload" to reload the page.

Implementation

As Page rendering is inseparable from interface requests during Project creation, I encapsulated wx. request to determine whether the network is disconnected and record this request after the network is disconnected.

/*** Obj request parameter * cb requrst request successfully returns * Current page instance */function wxRequest (obj, cb, page, type) {var isOne = true var cachFn = function () {wx. request ({url: obj. url, data: obj. data | {}, method: obj. method | 'GET', success: function (res) {cb. call (page, res) if (! Page. data. isNet) {page. setData ({isNet: true}) }}, // when fail is executed, fail: function () {// prevents fail from being executed twice, affecting rendering if (! IsOne) {return} page. setData ({isNet: false, isRequested: false}) // record this request. when loading, execute reloadFn of the page instance to get the page. reloadFn = wxRequest (obj, cb, page, 1) isOne = false }})} if (type) {page. isRequested = true} return type? CachFn: cachFn ()}
Application
Let data = {url: '', data :{}, method:''} wxTools. wxRequest (data, (res) =>{// data rendering this. setData ({})}, this)

When the network is disconnected, execute this. reloadFn to re-initiate the last request, isNet and determine whether the network is disconnected.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.