如何使用退货;有功能

时间:2017-06-22 20:20:53

标签: javascript

在我的程序的某些地方,如果数据不匹配,我会使用包含脚本的检查:

const a = 1;
//...
//...
if (param1 != 1 && param2 != 3) {
   return; // stop script
}
//...
//...
if (param1 != 1 && param2 != 3) {
   return; // stop script
}
//...
//...

所以我创建了函数:

function checkIfOk(param1, param2)
{
    if (param1 != 1 && param2 != 3) {
       return; // stop script
    }
}

但现在:

const a = 1;
//...
//...
checkIfOk(param1, param2);
//...
//...
checkIfOk(param1, param2);
//...
//...

不会在错误时暂停该功能。

const a = 1;
//...
//...
return checkIfOk(param1, param2);
//...
//...
return checkIfOk(param1, param2);
//...
//...

这会在任何情况下停止该功能。

我不想要使用throw错误,因为我在函数中使用它,所以程序应该仍在运行。

2 个答案:

答案 0 :(得分:0)

您需要返回一个值(例如falsetrue),并且仅在返回false

时停止

e.g。

function checkIfOk(param1, param2)
{
    if (param1 != 1 && param2 != 3) {
       return false; // stop script
    }
    return true; // OK!!
} 

if(!checkIfOk(param1, param2))
   return; //only return if checkIfOk returns false

答案 1 :(得分:0)

这是实现您想要的更快捷方式:

<ion-view class="app-tab" hide-nav-bar="true" view-title="Support">
    <ion-content class="padding">
        <h3 class="tab-header-text">Live Video Support</h3>
        <div id="call-connected">Connected to Live Tech Support</div>

        <div id="controls">
            <div id="preview" style="float:left;">
                <div id="local-media"></div>
                <button id="button-preview" style="margin-left:40px;">Preview My Camera</button>
            </div>
            <div id="call-controls" class="call-button-div">
                <i id="button-call" class="icon ion-ios-telephone-outline call-buttons" style="color:green;"></i>
                <i id="button-call-end" class="icon ion-ios-telephone-outline call-buttons" style="color:red; display:none;"></i>
            </div>
        </div>
        <div id="spin-wrapper"><ion-spinner name="circles"></ion-spinner></div>

        <div id="remote-media">
            <div id="video-overlay">Nicholas Kreidberg</div>
        </div>

    </ion-content>
</ion-view>

我猜在语义上它应该是angular.module('PROJECT_NAME.controllers').controller('SupportVideoCtrl', ['$rootScope', '$scope', '$http', function($rootScope, $scope, $http) { // instantiate Twilio Programmable Video library const Video = Twilio.Video; // setup some vars var activeRoom; var previewTracks; var identity; var roomName; // Attach the Tracks to the DOM. function attachTracks(tracks, container) { tracks.forEach(function(track) { container.appendChild(track.attach()); }); } // Attach the Participant's Tracks to the DOM. function attachParticipantTracks(participant, container) { var tracks = Array.from(participant.tracks.values()); attachTracks(tracks, container); } // Detach the Tracks from the DOM. function detachTracks(tracks) { tracks.forEach(function(track) { track.detach().forEach(function(detachedElement) { detachedElement.remove(); }); }); } // Detach the Participant's Tracks from the DOM. function detachParticipantTracks(participant) { var tracks = Array.from(participant.tracks.values()); detachTracks(tracks); } // When we are about to transition away from this page, disconnect // from the room, if joined. window.addEventListener('beforeunload', leaveRoomIfJoined); $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) { leaveRoomIfJoined(); } ); $http.get('TOKEN_URL', function(data) { }).then(function(data) { console.log(data); console.log("Token = " + data.data.token); //document.getElementById('room-controls').style.display = 'block'; // Bind click event and add token to data attribute document.getElementById('button-call').addEventListener('click', connect); document.getElementById('button-call').setAttribute('data-token', data.data.token); // Connect connect(); // Bind button to leave Room. document.getElementById('button-call-end').onclick = function() { log('Disconnecting...'); document.getElementById('call-connected').style.display = 'none'; document.getElementById('spin-wrapper').style.display = 'none'; document.getElementById('button-preview').style.display = 'block'; document.getElementById('video-overlay').style.display = 'none'; activeRoom.disconnect(); }; }); function connect() { roomName = 'Support'; log("Joining room '" + roomName + "'..."); token = document.getElementById('button-call').getAttribute('data-token'); console.log("Token: "+token); var connectOptions = { name: 'Support', logLevel: 'debug' }; if (previewTracks) { connectOptions.tracks = previewTracks; } // Join the Room with the token from the server and the // LocalParticipant's Tracks. Video.connect(token, connectOptions).then(roomJoined, function(error) { log('Could not connect to Twilio: ' + error.message); }); document.getElementById('call-connected').style.display = 'block'; document.getElementById('spin-wrapper').style.display = 'inline-flex'; document.getElementById('button-preview').style.display = 'none'; } // Successfully connected! function roomJoined(room) { window.room = activeRoom = room; log("Joined as '" + identity + "'"); document.getElementById('button-call').style.display = 'none'; document.getElementById('button-call-end').style.display = 'inline'; // Attach LocalParticipant's Tracks, if not already attached. var previewContainer = document.getElementById('local-media'); if (!previewContainer.querySelector('video')) { attachParticipantTracks(room.localParticipant, previewContainer); } // Attach the Tracks of the Room's Participants. room.participants.forEach(function(participant) { log("Already in Room: '" + participant.identity + "'"); var previewContainer = document.getElementById('remote-media'); attachParticipantTracks(participant, previewContainer); }); // When a Participant joins the Room, log the event. room.on('participantConnected', function(participant) { //document.getElementById('remote-media').style.display = 'inline'; log("Joining: '" + participant.identity + "'"); }); // When a Participant adds a Track, attach it to the DOM. room.on('trackAdded', function(track, participant) { log(participant.identity + " added track: " + track.kind); var previewContainer = document.getElementById('remote-media'); document.getElementById('spin-wrapper').style.display = 'none'; document.getElementById('video-overlay').style.display = 'flex'; attachTracks([track], previewContainer); }); // When a Participant removes a Track, detach it from the DOM. room.on('trackRemoved', function(track, participant) { log(participant.identity + " removed track: " + track.kind); detachTracks([track]); }); // When a Participant leaves the Room, detach its Tracks. room.on('participantDisconnected', function(participant) { log("Participant '" + participant.identity + "' left the room"); detachParticipantTracks(participant); }); // Once the LocalParticipant leaves the room, detach the Tracks // of all Participants, including that of the LocalParticipant. room.on('disconnected', function() { log('Left'); if (previewTracks) { previewTracks.forEach(function(track) { track.stop(); }); } detachParticipantTracks(room.localParticipant); room.participants.forEach(detachParticipantTracks); activeRoom = null; document.getElementById('button-call').style.display = 'inline'; document.getElementById('button-call-end').style.display = 'none'; document.getElementById('spin-wrapper').style.display = 'none'; }); } // Preview LocalParticipant's Tracks. document.getElementById('button-preview').onclick = function() { var localTracksPromise = previewTracks ? Promise.resolve(previewTracks) : Video.createLocalTracks(); localTracksPromise.then(function(tracks) { window.previewTracks = previewTracks = tracks; var previewContainer = document.getElementById('local-media'); if (!previewContainer.querySelector('video')) { attachTracks(tracks, previewContainer); } }, function(error) { console.error('Unable to access local media', error); log('Unable to access Camera and Microphone'); }); }; document.getElementById('mute').onclick = function() { console.dir(room.localParticipant); room.localParticipant.audioTracks.disable(); }; // Activity log. function log(message) { console.dir(message); return false; var logDiv = document.getElementById('log'); logDiv.innerHTML += '<p>&gt;&nbsp;' + message + '</p>'; logDiv.scrollTop = logDiv.scrollHeight; } // Leave Room. function leaveRoomIfJoined() { if (activeRoom) { activeRoom.disconnect(); } } }]); ,但是呃......