![]() System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.33 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, Directory : /var/www/html/libs/absol/demo/ |
Upload File : |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Thread</title> <script src="../dist/absol.js?date=<?php echo stat('../dist/absol.js')['mtime'];?>"></script> <link rel="stylesheet" href="https://absol.cf/hightlight/styles/default.css"> <script src="https://absol.cf/hightlight/highlight.pack.js"></script> </head> <body> <script class="viewable"> var myThread = new absol.Thread({ libs: ['https://cdnjs.cloudflare.com/ajax/libs/js-polyfills/0.1.43/polyfill.min.js'],//có thể thêm thư viện ngoài props: { myArray: [0, 1, 2, 3, 4, 5, 6]// tên thuộc tính không được trùng với các thuộc tính của global }, methods: { add: function (a, b) { return a + b; }, sortArr: function (arr) { arr.sort(); return arr; }, log: function () { console.log.apply(console, arguments); }, makeError: function () { //giả sử bên này chạy có lỗi throw new Error("Đây là lỗi bên worker") }, spliceArr: function () { //truy suất vào myArray, có thể gọi this.myArray, self.myArray, vì myArray trong global scope của worker return myArray.splice.apply(myArray, arguments); } }, extendCode: 'function echo(x, y, z){console.log(x, y, z)}//phần log này chạy bên worker\n' + 'function testLodash(){' + 'if ("_" in self) console.log("Load lodash OK!")' + '}' }); myThread.add(1, 3).then(function (result) { console.log('1 + 3 = ', result); }); myThread.add(4, 3).then(function (result) { console.log('4 + 3 = ', result); }); //ngoài ra có thể gọi như thế này myThread.invoke('add', 4, 3).then(function (result) { console.log('4 + 3 = ', result); }); myThread.sortArr([1, 5, 9, 8, 52, 54, 7, 7, 2, 0.5]).then(function (result) { console.log(result); }); //myThread.echo(1234)// nếu gọi thế này sẽ gây lỗi vì echo không có trong method myThread.invoke('echo', 1, 2, 3); myThread.log(window); //những object kiểu này không thể JSON.stringify thì không thể truyền đi được //load thêm code ngoài, hoặc thư viện ngoài, nếu thư viện đó hỗ trợ chạy trên worker myThread.importScriptURLs('https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js'); myThread.invoke('testLodash'); myThread.makeError().then(function () { console.log("Không chạy vào đây vì bên kia có lỗi"); }).catch(function (err) { console.error("Worker chạy lỗi", err) }); //import một đoạn code, tương tự như importScriptURLs myThread.importScript('console.log(self)'); myThread.spliceArr(2, 2).then(function (result) { console.log("lấy 2 phần tử từ vị trí số 2 được", result); //lấy một prop bên worker, không phải function myThread.invoke('myArray').then(function (result1) { console.log('còn lại', result1); }) }); //tạo thêm hàm mới myThread.createMethod('multiply', function (a, b) { return a + b; }); myThread.multiply(12, 11).then(function (result) { console.log('12 * 11 = ', result); }); myThread.log({ o: new Date() }); </script> <script src="./autohightlighting.js?"></script> </body> </html>