[WASM Rust] Use the js-sys Crate to Invoke Global APIs Available in Any JavaScript Environment
js-sys offers bindings to all the global APIs available in every JavaScript environment as defined by the ECMAScript standard.
In this lesson, we will install and use js-sys to invoke JavaScript's Date API to grab the current time. The date will need to be converted to a value Rust can use and display that date from Rust in the browser.
Cargo.toml:
[dependencies]
cfg-if = "0.1.5"
wasm-bindgen = "0.2.25"
js-sys = "0.2"
lib.rs:
// Called by our JS entry point to run the example
#[wasm_bindgen]
pub fn run() {
let now = js_sys::Date::now();
let now_date = js_sys::Date::new(&JsValue::from_f64(now)); let val = document.createElement("p"); val.set_inner_html(&format!(
"Hello from Rust, it's {}:{}",
now_date.get_hours(),
now_date.get_minutes()
));
document.body().append_child(val);
}
index.js:
import("../crate/pkg").then(module => {
module.run();
});
[WASM Rust] Use the js-sys Crate to Invoke Global APIs Available in Any JavaScript Environment的更多相关文章
- Day.js 是一个轻量的处理时间和日期的 JavaScript 库
Day.js 是一个轻量的处理时间和日期的 JavaScript 库,和 Moment.js 的 API 设计保持完全一样. 如果您曾经用过 Moment.js, 那么您已经知道如何使用 Day.js ...
- [WASM Rust] Create and Publish a NPM Package Containing Rust Generated WebAssembly using wasm-pack
wasm-pack is a tool that seeks to be a one-stop shop for building and working with Rust generated We ...
- [WASM + Rust] Debug a WebAssembly Module Written in Rust using console.log
Having some kind of debugging tool in our belt is extremely useful before writing a lot of code. In ...
- Ext JS学习第九天 Ext基础之 扩展原生的javascript对象
此文来记录学习笔记: •Ext对于原生的javascript对象进行了一系列的扩展,我们把他们掌握好,更能深刻的体会Ext的架构,从而对我们的web开发更好的服务, 源码位置,我们可以从开发包的这个位 ...
- js 计算当年还剩多少时间的倒数计时 javascript 原理解析【复制到编辑器查看推荐】
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JS的六大对象:Global、Math、Number、Date、JSON、console,运行在服务器上方的支持情况分析
在ASP中使用runat="server"来调用JS的相关函数,代码如下: <script runat="server" language="j ...
- face-api.js:一个在浏览器中进行人脸识别的 JavaScript 接口
Mark! 本文将为大家介绍一个建立在「tensorflow.js」内核上的 javascript API——「face-api.js」,它实现了三种卷积神经网络架构,用于完成人脸检测.识别和特征点检 ...
- js中获取一个对象里面的方法和属性的javascript
<script type="text/javascript"> var obj = { attribute:1, method:function() { alert(& ...
- 为什么很多国内公司不使用 jQuery 等开源 JS 框架(库),而选择自己开发 JavaScript 框架?
http://www.zhihu.com/question/20099586/answer/13971670 我对公司JAVASCRIPT框架的定位思考:
随机推荐
- 【转发】【linux】【ftp】CentOS 7.0安装配置Vsftp服务器
adduser -d /var/www/android -g ftp -s /sbin/nologin ftp2 一.配置防火墙,开启FTP服务器需要的端口 CentOS 7.0默认使用的是firew ...
- 【php】 自带的过滤机制
<?php print_r(filter_list()); ?> 输出类似: Array ( [0] => int [1] => boolean [2] => float ...
- 【windows】【php】【nginx】windows 开机自启动nginx php 及nginx php配置
#启动php-nginx start-php-nginx.bat @ECHO OFFECHO Starting PHP FastCGI...RunHiddenConsole.exe php-c ...
- 【php】子类覆盖超类方法,在超类里调用此方法会出现何种现象
<?php class A { public function getName() { echo $this->name(); } function name () { return 'l ...
- LeetCode(121) Best Time to Buy and Sell Stock
题目 Say you have an array for which the ith element is the price of a given stock on day i. If you we ...
- PAT Basic 1065
1065 单身狗 “单身狗”是中文对于单身人士的一种爱称.本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱. 输入格式: 输入第一行给出一个正整数 N(≤ 50 000),是已知夫妻/伴侣 ...
- java 邮箱验证
1.需要加入发送邮件的mail.jar: http://www.oracle.com/technetwork/java/javamail/index-138643.html 2.将字符串通过MD5进行 ...
- SQL Server数据库和MySQL数据库有什么区别?
SQL Server数据库和MySQL数据库有什么区别呢?详细很多初入IT行业的朋友对于SQL Server数据库和MySQL数据库经常搞混,认为这两种数据库是同一种,其实不然,今天我们来分析一下这两 ...
- ctype.h 第2章
ctype.h ctype.h是c标准函数库中的头文件 定义了一批c语言字符分类函数 (c character classification functions) 用于测试字符是否属于特定的字 ...
- ACM程序设计选修课——1081: 堆(BFS)
1081: 堆 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 26 Solved: 9 Description Input Output Sampl ...