简体   繁体   English

替换内容 <div> 等待时循环

[英]Replace contents of <div> in a loop while waiting

I'm having difficulty figuring out my issue, or even finding solutions for it, which leads me to believe I may be going in the wrong direction. 我很难弄清我的问题,甚至找不到解决方案,这使我相信我可能会走错了方向。

On a cshtml page I have an ajax function tied to a button click. cshtml页面上,我具有一个与按钮单击相关联的ajax函数。 That routes back to my controller and a json string array is returned back to the client. 路由返回到我的控制器,并且json字符串数组返回给客户端。

On the page itself I have <pre id="replaceMe">***</pre> defined. 在页面本身上,我定义了<pre id="replaceMe">***</pre> I am attempting to iterate through the json array doing $("#replaceMe").replaceWith('<pre id="replaceMe">' + data[i] + '</pre>'); 我试图通过做$("#replaceMe").replaceWith('<pre id="replaceMe">' + data[i] + '</pre>');的json数组进行迭代$("#replaceMe").replaceWith('<pre id="replaceMe">' + data[i] + '</pre>');

Technically speaking this works, but only in the sense that the last update is visible. 从技术上讲,这是可行的,但仅在可以看到最新更新的意义上。 I might as well just go straight to the last element of the array. 我也可以直接转到数组的最后一个元素。

I've tried using setTimeout to no avail, no changes and then suddenly the last element is displayed. 我尝试使用setTimeout无效,没有更改,然后突然显示了最后一个元素。 I've found some sleep like functions that mimic the same basic behavior, all with similar results. 我发现一些类似函数的sleep ,它们模仿相同的基本行为,而所有结果都相似。 I did see some recommendations for an async sleep , but none of my browsers seem to like the async and give me an error about a missing ; 我确实看到一些关于async sleep建议,但是我的浏览器似乎都不喜欢异步并给我一个关于丢失的错误; .

I then thought I could do something like 然后我以为我可以做类似的事情

function updateSection(data) {
    for (var i = 0; i < data.length; i++){
        var section = $("#replaceMe");
        section.fadeOut(500);
        section.replaceWith('<pre id="replaceMe">' + data[i] + '</pre>');
        section.fadeIn(500);
    }
}

That however has the same end result. 然而,这具有相同的最终结果。 No apparent change and then suddenly it's the last element in the array. 没有明显的变化,然后突然变成了数组中的最后一个元素。

I'm clearly going about this wrong, otherwise I'd find an example fairly readily I think, so what should I be doing instead? 我显然正在解决这个错误,否则我会很容易地找到一个示例,那么我应该怎么做呢?

To clarify and sum up, I want to replace the content of the <pre></pre> with text that's contained in an array. 为了澄清和总结,我想用数组中包含的文本替换<pre></pre>的内容。 I want each iteration to be visible long enough for a human to see it and observe the changes (~1000ms) before going to the next iteration. 我希望每个迭代都足够长的可见时间,以便人类可以看到它并观察变化(〜1000ms),然后再进行下一个迭代。

If, for example the array contains "Tom", "Dick", "Harry" , then I would like for the page to have 例如,如果数组包含"Tom", "Dick", "Harry" ,那么我希望页面具有

<pre id="replaceMe">Tom</pre> for 1 second, then that element is replaced with <pre id="replaceMe">Tom</pre> 1秒钟,然后将该元素替换为

<pre id="replaceMe">Dick</pre> for 1 second, then that element is replaced with <pre id="replaceMe">Dick</pre> 1秒钟,然后将该元素替换为

<pre id="replaceMe">Harry</pre>

I am NOT looking for 我不在找

<pre id="replaceMe">Tom</pre>
<pre id="replaceMe">Dick</pre>
<pre id="replaceMe">Harry</pre>

setTimeout in a for loop runs after the for loop execution completed. for循环中的setTimeout在for循环执行完成后运行。 so, you always see the last value. 因此,您总是看到最后一个值。 to solve this, you can use $.each method which provides a callback function or use an Immediately Invoked Function Expression. 要解决此问题,可以使用提供回调函数的$.each方法或使用立即调用的函数表达式。

more detailed info: https://codehandbook.org/understanding-settimeout-inside-for-loop-in-javascript/ 更详细的信息: https : //codehandbook.org/understanding-settimeout-inside-for-loop-in-javascript/

 var data=[]; for(var i=0; i<10; i++){ data.push(i+' lorem ipsum doloret'); } $.each(data, function(i, el){ setTimeout(function(){ $("#replaceMe").replaceWith('<pre id="replaceMe">' + data[i] + '</pre>'); },500 + ( i * 1000 )); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <pre id="replaceMe">***</pre> 

You can do that using setInterval function: 您可以使用setInterval函数做到这一点:

var words = ['interating', 'and', 'replacing', 'text'];
var replace = document.querySelector('#replace');
var count = 0;

function replaceText() {
  replace.innerHTML = words[count];
  if(count === words.length - 1)
    count = 0;
  else
    count++;
}

setInterval(replaceText, 1000);

Updating: You don't need to replace all the element, you can replace only the content, using the atribute innerText. 更新:不需要替换所有元素,可以使用属性innerText替换仅内容。

 //pass in the data to loop over, and the index to show function updateSection(data, index) { //get a reference to the section var $section = $('#replaceMe'); //fade out the section over 500 milliseconds, then perform callback on finish $section.fadeOut(500, () => { //change the text $section.text(data[index]); //fade in the section over 500 milliseconds, and then advance the index //use modulus to reset the index to 0 when it reaches beyond the array $section.fadeIn(500, () => updateSection(data, ++index % data.length)); }); } updateSection(['Tom', 'Dick', 'Harry'], 0); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="replaceMe">***</div> 

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

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