uni-app setIntervalWeex is not defined

发布于 1周前 作者 bupafengyu 来自 Uni-App

uni-app setIntervalWeex is not defined

开发环境 版本号 项目创建方式
HBuilderX 3.4.7
产品分类:HTML5+
手机系统:iOS
手机系统版本号:iOS 15
手机厂商:苹果
手机机型:iphone7
打包方式:云端

示例代码:

```javascript
function readTempFilePath(tempPath, callback) {  
    plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function(fs) {  
        let path = plus.io.convertLocalFileSystemURL(tempPath)  
        fs.root.getFile(path, {  
            create: false  
        }, function(fileEntry) {  
            fileEntry.file(function(file) {  
                var fileReader = new plus.io.FileReader();  
                fileReader.readAsText(file, 'utf-8');  
                fileReader.onloadend = function(evt) {  
                    console.log('读取成功')  
                }  
            });  
        }, function(err) {  
            console.log('err', err)  
        });  
    }, function(err) {  
        console.log('err', err)});  
}

let promiseItem = new Promise((resolve, reject) => {  
    readTempFilePath(bookStorage.downList[startIndex].file, fileRes1 => {  
        if (fileRes.code == 0) {  
            console.log('读取文件成功1', fileRes1)  

            resolve(fileRes1)  

        } else {  
            reject(false)  
        }  
    })

Promise.all([promiseItem, promiseItem, ...]).then(res => {  
    console.log(res)  
}).catch(e => {  
    console.log('报错了', e)  
    callback({code: 1})  
})

操作步骤: 多个promiseItem放入promise.all中调用

预期结果: 打印所有读取的文件内容

实际结果: Promise.all执行catch,打印报错了

报错了 ReferenceError: setIntervalWeex is not defined  
    at __dc__setInterval (eval at <anonymous> (Runtime.IOS.js:1), <anonymous>:4:665)  
    at Object.plusContext.setTimeout (eval at <anonymous> (Runtime.IOS.js:1), <anonymous>:4:997)  
    at Object.requestFileSystem (eval at <anonymous> (Runtime.IOS.js:1), <anonymous>:4:117692)  
    at readTempFilePath (fileUtil.js:2)  
    at eval (fileUtil.js:208)  
    at new Promise (<anonymous>)  
    at Object.saveDownloadFile (fileUtil.js:207)  
    at Function.success (read.vue:1367)  
    at   
    at Object.callback ()  uni-app:///util/fileUtil.js:254

bug描述: 调用 plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function(fs) {})
报错 ReferenceError: setIntervalWeex is not defined


5 回复

你这个问题解决了吗,我光是调用这个api就报这个问题


我现在也遇到这个问题,请问问题解决了吗

同样问题 调用getLocation方法后 在ios模拟器报错setIntervalWeex is not defined

请问这个问题有解决吗

The error “Weex is not defined” in uni-app typically occurs when you are trying to use Weex-specific APIs or features in a non-Weex environment. uni-app is a cross-platform framework that supports multiple platforms, including H5, WeChat Mini Programs, and native apps (via Weex). However, not all platforms support Weex-specific features.

Understanding the Issue

  • Weex: A framework for building high-performance mobile apps using Vue.js. It is used in uni-app for native app development.
  • setInterval: A JavaScript function that repeatedly calls a function or executes a code snippet, with a fixed time delay between each call.

If you are using setInterval in a uni-app project and encounter the “Weex is not defined” error, it is likely because the code is being executed in an environment where Weex is not available (e.g., H5 or a Mini Program).

Solution

To avoid this error, you should check the environment before using Weex-specific features. Here’s how you can do it:

  1. Check the Platform: Use uni-app’s platform detection to determine the current environment.
// Check if the platform is Weex (native app)
if (uni.getSystemInfoSync().platform === 'ios' || uni.getSystemInfoSync().platform === 'android') {
    // Weex-specific code
    setInterval(() => {
        console.log('This is running in a Weex environment');
    }, 1000);
} else {
    // Code for other platforms (H5, Mini Programs, etc.)
    setInterval(() => {
        console.log('This is running in a non-Weex environment');
    }, 1000);
}
  1. Use Uni-app’s Cross-Platform APIs: Whenever possible, use uni-app’s cross-platform APIs instead of platform-specific ones. For example, use uni.setTimeout or uni.setInterval instead of the native setTimeout or setInterval.
uni.setInterval(() => {
    console.log('This is running in any environment');
}, 1000);
  1. Conditional Compilation: Use conditional compilation to include or exclude code based on the platform.
// #ifdef APP-PLUS
// Weex-specific code
setInterval(() => {
    console.log('This is running in a Weex environment');
}, 1000);
// #endif

// #ifndef APP-PLUS
// Code for other platforms (H5, Mini Programs, etc.)
setInterval(() => {
    console.log('This is running in a non-Weex environment');
}, 1000);
// #endif
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!