Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
At present, there is a scenario where we need to return to the previous page after information is added, but a toast message is submitted successfully, but the toast message flashes by.
Question why
Wx.showToast only works on the current page, so when you add a new page, the toast prompt is not obvious or not visible at all because you immediately return to the previous page
The solution
To solve this problem, we can add the delay function after the new success, the specific code is as follows
success: function (res) {
console.log(res.data);
wx.showToast({
title: 'Thank you for your contribution! '.icon: "none".duration: 500,})setTimeout(function(){
wx.navigateBack({
delta: 1 // The applet closes the current page and returns to the previous page})},500)},Copy the code
After entering the request, we added in the success callback function that we should first write the code of showToast and then set the delay function. It is suggested that the delay function should be consistent with the popup time of showToast. So before the request is successful return back surface can for message bounced tips, but this way should pay attention to the function of the current operation, if you are in the form of submission, must pay attention to due to the delay, may cause the user click the phenomenon, to do the duplicate submissions, prevent new more than the same data.
ShowToast completely invalid problem solved
In addition to a flash scenario, there is another case where it does not work directly. This depends on other factors, such as whether wx.showLoading is used in the same method. The two cannot be used simultaneously
The two can not be used at the same time. If there is such a scenario, after investigating showLoading, call hideLoading to hide and then call showToast to take effect