我在node.js文件系统中遇到了问题.这是我的代码.我的函数总是返回一个空字符串.我想知道是否有任何方法使我的函数停止执行,直到readFile方法完成.
var fs = require('fs');
function myfun(filePath){
var str = '';
fs.readFile(filePath, function(err, data){
if(err) throw err;
str = data;
});
return str; //here, the variable str always return '' because the function doesn't wait for the readFile method complete.
}
Run Code Online (Sandbox Code Playgroud)
添加说明
实际上我正在做这样的事情:函数myfun用于替换str你可以看到我的代码:
function fillContent(content) {
var rex = /\<include.*?filename\s*=\s*"(.+?)"\/>/g;
var replaced = fileStr.replace(rex, function (match, p1) {
var filePath = p1
var fileContent = '';
fs.readFile(filePath, function (err, data) {
if (err) {
throw err;
}
fileContent = data; …Run Code Online (Sandbox Code Playgroud) 我想要一个带有如下图像序列的基于滚动的动画:
http://www.clearmotion.com/technology
这个动画看起来很流畅
我试过这个小提琴
var images = new Array();
var currentLocation = 0;
var totalImages = 200;
for (var i = 1; i < totalImages; i++) {
var img = new Image;
var slug = '000' + i;
img.src = 'https://s3.amazonaws.com/clearmotion/hero/high-min/frame'+ slug.slice(-3) +'-low.jpg'
images.push(img);
}
var c = document.getElementById("background");
var ctx = c.getContext("2d");
var mouseWheel = function () {
window.addEventListener('mousewheel', function (e) {
e.preventDefault(); // No scroll
// The following equation will return either a 1 for scroll down
// …Run Code Online (Sandbox Code Playgroud)