You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
chatai/dist/dev/mp-weixin/hooks/usePolling.js

59 lines
1.2 KiB

"use strict";
const common_vendor = require("../common/vendor.js");
function usePolling(fun, options) {
const result = common_vendor.ref(null);
const error = common_vendor.ref(null);
const {
time = 2e3,
totalTime,
count,
callback = () => false
} = options || {};
let timer = null;
let endTime = null;
let totalCount = 0;
const run = () => {
console.log("count2:", count);
if (endTime && endTime <= Date.now()) {
console.log("结束??");
end();
callback();
return;
}
if (count && totalCount >= count) {
console.log("结束??");
end();
callback();
return;
}
totalCount++;
timer = setTimeout(() => {
fun().then((res) => {
result.value = res;
run();
}).catch((err) => {
error.value = err;
});
}, time);
};
const start = () => {
endTime = totalTime ? Date.now() + totalTime : null;
console.log("不是运行了吗??");
run();
};
const end = () => {
setTimeout(() => {
clearTimeout(timer);
timer = null;
endTime = null;
totalCount = 0;
}, 0);
};
return {
start,
end,
error,
result
};
}
exports.usePolling = usePolling;