简体   繁体   English

如何使多个JavaScript函数的输出以HTML显示?

[英]How to make the output of multiple JavaScript functions display in HTML?

So I'm trying to display the results of a few function calls in a JavaScript file that uses benchmark.js in a separate HTML file. 因此,我试图在一个JavaScript文件中显示一些函数调用的结果,该JavaScript文件在单独的HTML文件中使用Benchmark.js。 My js file looks something like this (disregard the names of methods and classes): 我的js文件看起来像这样(忽略方法和类的名称):

class.init(function(context) {

    Benchmark("function description", {
        'defer': true,

         fn': function(deferred) {
         var x = context.ones([100, 100]);
         var y = x.repeat(2, 0);
         context.barrier(function (){
             deferred.resolve();
          });
    },
    'onComplete': function(event) {
    //This is what I'd like to print out
    console.log(this.name + ": " + (this.stats.mean * 1000).toFixed(2) + " ms");
    //
    } 
}).run();

There are multiple function calls similar to this. 有多个与此类似的函数调用。

My HTML just looks something lile: 我的HTML看起来很卑鄙:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

        <title> Sublime Webpage </title>

    </head>
    <body>
        <div class="main">
          <div class="head">
             <h1>Hello</h1>

          </div>
          <script src="filename.js"></script>
        </div>
    </body>
</html>

At the moment, it just prints the results to the console, which is better than nothing, but obviously not what I want. 目前,它只是将结果打印到控制台,这总比没有好,但显然不是我想要的。 I talked to someone briefly and they suggested I use jQuery. 我与某人简短交谈,他们建议我使用jQuery。 I looked into it, and tried using document.write(this.name + ": " + (this.stats.mean * 1000).toFixed(2) + " ms") in the place of console.log(), but this didn't seem to work. 我调查了一下,并尝试使用document.write(this.name +“:” +(this.stats.mean * 1000).toFixed(2)+“ ms”)代替console.log(),但是这似乎没有用。 Does anyone have suggestions? 有人有建议吗?

Use document.createTextNode : 使用document.createTextNode

// add an output div to your html
<div id='output'></div>

// in your benchmark code
var output = document.getElementById('output');
output.appendChild(document.createTextNode('some result'));

Fiddle 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM