如何同步调用字符串中的函数

时间:2018-03-29 14:24:18

标签: javascript jquery

我正在尝试同步运行一串函数调用 从提供的示例中可以看出,函数被调用但是异步。

在示例中,以下是hanppens:

1 |调用函数“REMOVE”,它删除第4列中具有分号长度,等于零的所有行,因此删除示例中的第2行

2 |调用函数“B1”,它从左边改变第3个单元格的单元格值,从顶部改变第4个单元格的单元格值 如果代码同步工作,单元格“C5”将具有值“X”,但在下面的示例中,“X”被放置在单元格“C4”中。
这是因为运行删除单元格的代码所需的时间比单元格值的更改要长。

这只是从字符串中调用的一些函数的示例,但还有更多。

我尝试使用setTimeOut,但没有解决问题 setTimeout(window[routines[i][0]](routines[i][1]),0)

我的问题是,有没有办法同步调用字符串中的所有函数?

	function routineConverter(v){
    v = '["REMOVE",["REMOVE","ROW","-","4","HAS A LENGTH OF","=","null","0","","4"]],["B1",["X",2,3]]'
		var routines = JSON.parse('[' + v + ']');//CONVERT STRING TO ARRAY
		for ( var  i=0; i < routines.length ; i++ ){
			window[routines[i][0]](routines[i][1])
		}
	}
  
 		function B1(p){ //CHANGE CELL VALUE
			$('#tbl').find('tr:eq(' + p[2] + ')').find('td:eq(' + p[1] + ')').text(p[0]);
		}
    
   //CONDITIONAL ACTIONS 
   function REMOVE(p){
		var A = p[9] //SELECTED COLUMN
		var B = p[1] //ROW,VALUE,STRING
		var C = p[3]-1 //COLUMN TO APPLY CONDITION ON
		var D = p[4] //CONDITION
		var E = p[5] //=,<,>
		var F = p[6] //ARGUMENT
		var G = p[7] //ARGUMENT VALUE
		var H = p[2] //TO BE REPLACE/APPEND/REPLACE VALUE
		var K = p[8] //REPLACE WITH VALUE
		$('table tbody tr td:nth-child(' + A + ')').each(function(index){
			var cellValue = $("#tbl tr:eq(" + index + ") td:eq(" + C + ")").text();
			var stringIndex = cellValue.indexOf(G);
			var tmp = false;

			switch (D) {
				case 'CONTAINS'://CONTAINS
					switch (F) {
						case 'A STRING':// USER DEFINED STRING
							if (stringIndex > -1){tmp = true;}
							break;
						case 'NUMBERS'://CONTAINS ANY NUMBERS
							if (cellValue.match(/\d+/g) != null){tmp = true;}
							break;
						case 'NUMBERS ONLY'://CONTAINS ONLY NUMBERS
							if ($.isNumeric(cellValue)){tmp = true;}
							break;
						case 'TEXT ONLY'://DOES NOT CONTAIN NUMBERS
							if (cellValue.match(/\d+/g) === null){tmp = true;}
					}
					break;
				case 'DOES NOT CONTAIN'://DOES NOT CONTAIN
					if (stringIndex == -1){tmp = true;}
					break;
				case 'HAS A LENGTH OF'://LENGTH EQUALS
					if (cellValue.length == G && E === '='){tmp = true;}
					if (cellValue.length > G && E === '&gt;'){tmp = true;}
					if (cellValue.length < G && E === '&lt;'){tmp = true;}
					break;
				case 'STARTS WITH'://STARTS WITH
					if (cellValue.substring(0, G.length) == G){tmp = true;}
					break;
				case 'ENDS WITH'://ENDS WITH
					if (cellValue.lastIndexOf(G) == cellValue.length - G.length){tmp = true;}
			}
			if (tmp === true && B === 'VALUE'){
				var X = this.innerHTML.replace(this.innerHTML, '');
				this.innerHTML = X;
			}
			if (tmp === true && B === 'ROW'){
				$(this).parents('tr').fadeOut(function(){
					$(this).remove();
				});
			}
		});
	}
table {
    margin: 10px;
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 95%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id='tbl'>
  <tr>
    <td>A1</td>
    <td>B1</td>
    <td>C1</td>
    <td>D1</td>
    <td>E1</td>
    <td>F1</td>
    <td>G1</td>
    <td>H1</td>
  </tr>
  <tr>
    <td>A2</td>
    <td>B2</td>
    <td>C2</td>
    <td></td>
    <td>E2</td>
    <td>F2</td>
    <td>G2</td>
    <td>H2</td>
  </tr>
  <tr>
    <td>A3</td>
    <td>B3</td>
    <td>C3</td>
    <td>D3</td>
    <td>E3</td>
    <td>F3</td>
    <td>G3</td>
    <td>H3</td>
  </tr>
  <tr>
    <td>A4</td>
    <td>B4</td>
    <td>C4</td>
    <td>D4</td>
    <td>E4</td>
    <td>F4</td>
    <td>G4</td>
    <td>H4</td>
  </tr>
  <tr>
    <td>A5</td>
    <td>B5</td>
    <td>C5</td>
    <td>D5</td>
    <td>E5</td>
    <td>F5</td>
    <td>G5</td>
    <td>H5</td>
  </tr>
  <tr>
    <td>A6</td>
    <td>B6</td>
    <td>C6</td>
    <td>D6</td>
    <td>E6</td>
    <td>F6</td>
    <td>G6</td>
    <td>H6</td>
  </tr>
    <tr>
    <td>A7</td>
    <td>B7</td>
    <td>C7</td>
    <td>D7</td>
    <td>E7</td>
    <td>F7</td>
    <td>G7</td>
    <td>H7</td>
  </tr>
    <tr>
    <td>A8</td>
    <td>B8</td>
    <td>C8</td>
    <td>D8</td>
    <td>E8</td>
    <td>F8</td>
    <td>G8</td>
    <td>H8</td>
  </tr>
    <tr>
    <td>A9</td>
    <td>B9</td>
    <td>C9</td>
    <td>D9</td>
    <td>E9</td>
    <td>F9</td>
    <td>G9</td>
    <td>H9</td>
  </tr>
    <tr>
    <td>A10</td>
    <td>B10</td>
    <td>C10</td>
    <td>D10</td>
    <td>E10</td>
    <td>F10</td>
    <td>G10</td>
    <td>H10</td>
  </tr>
    <tr>
    <td>A11</td>
    <td>B11</td>
    <td>C11</td>
    <td>D11</td>
    <td>E11</td>
    <td>F11</td>
    <td>G11</td>
    <td>H11</td>
  </tr>
</table>
</br>
 <button type="button" onclick="routineConverter()">Click Me!</button>

0 个答案:

没有答案