Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中使用.length重复函数_Javascript_Html - Fatal编程技术网

在Javascript中使用.length重复函数

在Javascript中使用.length重复函数,javascript,html,Javascript,Html,我有一根长长的绳子 var string = "This is a long string." 我还有一个函数可以将字符串下载到“path” 如何对字符串的每两个字母执行此函数?阅读有关使用“.length”的内容,但不确定在这种情况下如何实现它。我也不想将字符串拆分为数组。下载功能应该有助于拆分字符串以逐步下载2个字母 也就是说,我想一次下载两个字母的字符串 编辑:为了澄清,字符串需要一次下载x个字符,因为如果超过该限制,下载将中断。计算字符串长度 除以2向上或向下取整你喜欢的 然后在除法后

我有一根长长的绳子

var string = "This is a long string."
我还有一个函数可以将字符串下载到“path”

如何对字符串的每两个字母执行此函数?阅读有关使用“.length”的内容,但不确定在这种情况下如何实现它。我也不想将字符串拆分为数组。下载功能应该有助于拆分字符串以逐步下载2个字母

也就是说,我想一次下载两个字母的字符串

编辑:为了澄清,字符串需要一次下载x个字符,因为如果超过该限制,下载将中断。

计算字符串长度 除以2向上或向下取整你喜欢的

然后在除法后做一个for循环

像这样的东西

//Your string that is downloaded
var string = "This is a long string."

//calculate the amount of character in the string
var amount = string.length;

//divide the string by 2
var roundnr = amount/2;

for (var i=0;i<Math.round(roundnr);i++)
{ 
//do something here for every 2 characters in the string
}
//下载的字符串
var string=“这是一个长字符串。”
//计算字符串中的字符数
var amount=string.length;
//将字符串除以2
var roundnr=金额/2;

对于(var i=0;i,这里有一个示例,说明了如何执行此操作:

var string = 'a really really really long string \
    that I want to split by two letters at a time';

// this needs to be ceiling so that it always rounds up on odd numbers
// otherwise the last letter may be left out
var iterations = Math.ceil(string.length / 2);

for (var i = 0; i < iterations; i++) {

    // we are iterating over half of the length but want to go two letters at a time
    var j = i*2;

    // make a new string with the first letter
    var letters = string[j]

    // this is an if statement to check if there is a second letter
    // if there is concat it to the first letter
    // otherwise the last set of an odd length would concat `undefined` to it
    if (string[j+1]) { letters += string[j+1]; }

    // call your function here passing `letters` to it
    downloadfunction{... 'echo "' + letters + '" >> '+path;}
}
var string='一个非常长的字符串\
我想一次分成两个字母';
//这需要设置为上限,以便它总是在奇数上取整
//否则最后一个字母可能会被省略
var迭代=Math.ceil(string.length/2);
对于(var i=0;i>'+路径;}
}

您可以使用
循环字符串,用于
循环并按索引访问字符。然后使用每x个字母执行一次操作。您所说的“下载字符串中的2个字母”是什么意思?因此…字符串只是某个服务器端文件中的文本,您想逐字节下载?否则我不理解您所说的“下载字符串”是什么意思.Jack,Felix,我已经澄清了我的问题。我需要每2个字符/字节下载一次字符串,而不是批量下载。我理解。但你的措辞没有意义。你不能“下载字符串”。您可以下载文件。或者您可以访问URL并流式传输URL返回的任何内容。如果字符串位于JavaScript源文件中,并且您通过
包含该文件,则该文件将立即完全下载。
var string = 'a really really really long string \
    that I want to split by two letters at a time';

// this needs to be ceiling so that it always rounds up on odd numbers
// otherwise the last letter may be left out
var iterations = Math.ceil(string.length / 2);

for (var i = 0; i < iterations; i++) {

    // we are iterating over half of the length but want to go two letters at a time
    var j = i*2;

    // make a new string with the first letter
    var letters = string[j]

    // this is an if statement to check if there is a second letter
    // if there is concat it to the first letter
    // otherwise the last set of an odd length would concat `undefined` to it
    if (string[j+1]) { letters += string[j+1]; }

    // call your function here passing `letters` to it
    downloadfunction{... 'echo "' + letters + '" >> '+path;}
}